Hacks
%%js
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var alphabetList = [];
for (var i = 0; i < alphabet.length; i++) { // Use alphabet.length to iterate over the characters
alphabetList.push(alphabet[i]); // Push characters from the 'alphabet' string into the list
}
console.log(alphabetList);
<IPython.core.display.Javascript object>
What I changed
- Changed the loop condition to go through the characters in the ‘alphabet’ string again
- Pushed the characters from the ‘alphabet’ string into the ‘alphabetList’ array
%%js
// Copy your previous code to build alphabetList here
let alphabet = "abcdefghijklmnopqrstuvwxyz";
let letterNumber = 5;
for (var i = 0; i < alphabet.length; i++) { // Use alphabet.length to iterate over the characters
if (i + 1 === letterNumber) { // Compare (i + 1) with letterNumber
console.log(alphabet[i] + " is letter number " + letterNumber + " in the alphabet"); // Print the corresponding letter and position
}
}
<IPython.core.display.Javascript object>
What I changed
- Used the ‘alphaet’ string and its length to go through the characters again
- Corrected the comparision to check if (i+1) is equal to ‘letterNumber’
- Printed the cooresponding letter and position in the alphabet
%%js
let odds = []; // Changed the variable name to 'odds'
let i = 1; // Start from 1 to include odd numbers
while (i <= 10) {
odds.push(i); // Push odd numbers into 'odds'
i += 2; // Increment by 2 to get the next odd number
}
console.log(odds);
<IPython.core.display.Javascript object>
What I changed
- Changed the variable name from ‘evens’ to ‘odds’
- Started ‘i’ from 1 to include odd numbers.
- Made ‘i’ go by two to get all the odd numbers.
%%js
var numbers = [];
var newNumbers = [];
var i = 1; // Start from 1 to include numbers between 1 and 100
while (i <= 100) { // Changed the loop condition to include numbers up to 100
numbers.push(i);
i += 1;
}
for (var num of numbers) { // Used 'num' as the loop variable
if (num % 5 === 0 || num % 2 === 0) { // Check for multiples of 2 or 5
newNumbers.push(num);
}
}
console.log(newNumbers);
<IPython.core.display.Javascript object>
What I changed
- Changed the loop to go through numbers up to 100
- Used ‘num’ as the loop variable
- Checked for variables of 2 or 5
%%js
if (keys['ArrowDown'] && player.y < canvas.height) {
player.y += player.speed;
}
<IPython.core.display.Javascript object>
The original code didn’t account for the player’s size when checking if it’s at the bottom of the canvas, which causes a rendering issue
%%js
if (keys['ArrowDown'] && player.y < canvas.height - 20) {
player.y += player.speed;
}
<IPython.core.display.Javascript object>
I subtracted the player’s height from the canvs, to prevent the rendering issue.