These lines of code were taken and edited/revised to function better from the teacher repository. I accomplished this by using various for loops abd functions to loop through lists and strings. I also had to get a little bit creative to find the solutions to how to make a certain thing, for example choosing one of many ways to print out odd numbers, and using a while loop instead of for + if/else etc.
%%js
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];
for (var i = 0; i < 26; i++) {
alphabetList.push(alphabet.charAt(i));
}
console.log(alphabetList);
<IPython.core.display.Javascript object>
%%js
let letterNumber = 5
for (var i = 0; i < alphabetList.length; i++) {
if (i === letterNumber-1) {
console.log(alphabetList[letterNumber-1] + " is letter " + letterNumber + " in the alphabet")
}
}
let odds = [];
let n = 1;
while (n <= 10) {
if (n % 2 != 0) {
odds.push(n)
}
n += 2
}
console.log(odds);
All I had to do for this fourth (not required) section was to change the if in if (numbers[i] % 2 === 0) to else, making it so that it only checks numbers that are not divisible by 5, removing the overlap.
%%js
var numbers = []
var newNumbers = []
var i = 0
while (i < 100) {
numbers.push(i)
i += 1
}
for (var i of numbers) {
if (numbers[i] % 5 === 0)
newNumbers.push(numbers[i])
else if (numbers[i] % 2 === 0)
newNumbers.push(numbers[i])
}
console.log(newNumbers)
For this final challenge, I grabbed the corresponding values of the keys in one map that I took from a list that defines what is being bought. I then outputted all of the keys added together since they are integers, and also printed out the buy list in one message with concatenation.
%%js
var menu = {"burger": 3.99,
"fries": 1.99,
"drink": 0.99}
var total = 0
console.log("Menu")
for (var item in menu) {
console.log(item + " $" + menu[item].toFixed(2))
}
var buy = ["burger", "fries", "drink", "drink"]
for (var i=0; i < buy.length; i++){
total = total+menu[buy[i]]
}
console.log("Your total is: $"+ total.toFixed(2)+ ". Your items are: " + buy)