Segment 1: Alphabet List
Intended behavior: create a list of characters from the string contained in the variable alphabet
Code:
%%js
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];
for (var i = 0; i < alphabet.length; i++) {
alphabetList.push(alphabet[i]);
}
console.log(alphabetList);
Change 10 to alphabet.length in order for the “for” loop to run as long as “i” is less than the length of the alphabet string. Change (i) to (alphabet[i]) so that inside the loop, each letter is pushed into the alphabetlist.
Segment 2: Numbered Alphabet
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.)
Code:
%%js
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];
for (var i = 0; i < alphabet.length; i++) {
alphabetList.push(alphabet[i]);
}
let letterNumber = 5;
for (var i = 0; i < alphabetList.length; i++) {
if (i + 1 === letterNumber) {
console.log(alphabetList[i] + " is letter number " + letterNumber + " in the alphabet")
}
}
// Should output:
// "e" is letter number 5 in the alphabet
Change alphabetList to alphabetList.length in the second “for” loop to interate through the alphabetList. Change (i === letterNumber) to (i + 1 === letterNumber) in order to account for zero based counting.
Segment 3: Odd Numbers
Intended behavior: print a list of all the odd numbers below 10
Code:
%%js
let odds = [];
let i = 1;
while (i < 10) {
odds.push(i);
i += 2;
}
console.log(odds);
Change all “evens” to “odds” because we want odd numbers, not even Change i = 0 to i = 1 to start with the first odd number below 10 (1) Change i <= 10 to i < 10 because 10 is an even number