Home HTML Hacks Data Types Hacks DOM Hacks JavaScript Hacks JS Debugging Hacks

Segment 1

Intended behavior: create a list of characters from the string contained in the variable alphabet

%%js

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];

for (var i = 0; i < 26; i++) {
	alphabetList.push(alphabet.substring(i,i+1));
}

console.log(alphabetList);

for (var i = 0; i < 10; i++) {
	alphabetList.push(i);
}

<IPython.core.display.Javascript object>

What I Changed (Segment 1)

  • I changed the for loop to use values from 0-26 instead of 0-10 because the alphabet is 26 letter long.
  • I also changed the alphabestlist.push from just i (which would push 10 numbers) to alphabet.substring(i,i+1) which would recursively get the substring of each character from the original string and push that into the list

Segment 2

Intended behavior: print the number of a given alphabet letter within the alphabet. For example:

"_" is letter number _ in the alphabet

Where the underscores (_) are replaced with the letter and the position of that letter within the alphabet (e.g. a=1, b=2, etc.)

%%js

var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];

for (var i = 0; i < 26; i++) {
	alphabetList.push(alphabet.substring(i,i+1));
}

for (var i = 0; i < 10; i++) {
	alphabetList.push(i);
}

let letterNumber = 5

for (var i = 0; i < alphabetList.length; i++) {
	if (i === letterNumber) {
		console.log(alphabetList[letterNumber-1] + " is letter number 5 in the alphabet")
	}
}
<IPython.core.display.Javascript object>

What I Changed (Segment 2)

  • I changed the for loop to i less than alphabetList.length instead of just alphabetList because you want to go through every item in the list not just the name which would run nothing as it is not a variable for a number
  • I changed the console log output to alphabetList[letterNumber-1] instead of just letterNumber because you needed to call the letterNumber from the overall list and subtract one because lists starting counting from 0 not 1
  • I changed the console log output to read “letter number 5” not “letter number 1” because e is the 5th letter in the alphabet not 1st

Segment 3

Intended behavior: print a list of all the odd numbers below 10

%%js

let odds = [];
let i = 1;

while (i <= 10) {
  odds.push(i);
  i += 2;
}

console.log(odds);
<IPython.core.display.Javascript object>

What I Changed (Segment 3)

  • I changed the name for the array from evens to odds because we wanted odds not evens
  • I changed the value for starting i to 1 not 0 becuase we wanted odds not evens
  • I replaced evens with odds everywhere it occured because I changed the array name before