Inventors - Iterations Lesson HW
Categories: JavaScriptThese are the homework problems and popcorn hacks.
- Hack 1: The “Counting Stars” Hack (The while Loop)
- Hack 2: The “Playlist Shuffle” Hack (The For Loop) 🎵
Hack 1: The “Counting Stars” Hack (The while Loop)
Instruction: To run this hack, call the countStars() function with a number to set the limit for the star count.
%%html
<div id="outputPH1">Results will appear here:<br></div>
<script>
(() => {
const output = document.getElementById("outputPH1");
function countStars(limit) {
// 1. Initialize a counter variable. We'll start our count at 1.
let count = 1;
// 2. Start the `while` loop. The code inside this loop will run
// as long as the condition `count <= limit` is true.
function lightStar() {
if (count <= limit) {
// 3. This is the code that runs in each iteration.
console.log(`⭐ Star #${count} shining bright!`);
output.innerText += `⭐ Star #${count} shining bright!\n`;
// 4. This is the increment step. It's crucial! If we don't
// increase the `count`, the loop will run forever (an infinite loop).
count++;
// Call the function again after a short delay
setTimeout(lightStar, 200);
} else {
// 5. This message is displayed once the loop condition is no longer true
// and the loop has finished.
console.log("All the stars have been counted.");
output.innerText += "All the stars have been counted.\n";
}
}
lightStar();
}
// Example run
countStars(10);
})();
</script>
Results will appear here:
Hack 2: The “Playlist Shuffle” Hack (The For Loop) 🎵
Instruction: To run this hack, define an array of strings called mySongs and pass that array into the playlistShuffle() function.
%%html
<div id="outputPH2">Playlist Shuffle Results:<br></div>
<script>
(() => {
const output = document.getElementById("outputPH2");
function playlistShuffle(playlist) {
// 1. Start the `for...of` loop. This loop is perfect for iterating
// over the items of an array.
// The variable `song` will hold the value of each item in the `playlist` array, one by one.
for (const song of playlist) {
// 2. Action performed for each song in the array.
console.log(`Now playing: "${song}"`);
output.innerText += `Now playing: "${song}"\n`;
}
// 3. This message is displayed once the loop has processed every item in the array.
console.log("All songs have been played!");
output.innerText += "All songs have been played!\n";
}
// Example of how to define the array:
const mySongs = [
"Drake - God's Plan",
"Kendrick Lamar - Alright",
"J. Cole - Middle Child",
"Travis Scott - SICKO MODE",
"Post Malone - Circles",
"Lil Baby - In A Minute",
"Doja Cat - Woman",
"The Weeknd - Blinding Lights",
"Kanye West - Stronger",
"Ariana Grande - 7 Rings"
];
// Then, call the function:
playlistShuffle(mySongs);
})();
</script>
Playlist Shuffle Results:
Submission
You will submit the link to your homework on a web page in the below form.
If you are unable to get your homework accessible from the website, you can upload this Jupyter notebook to the form.
IMPORTANT: If uploading, please name this Jupyter notebook in this format: [FirstName][LastName]_iterations_hw.ipynb
https://forms.gle/yaiYRXDq341kb2538