3.8 Iterations Homework in JavaScript

Complete the 4 tasks below as homework:

  1. Uncomment the base code so you can work on adding what you need to produce the correct outputs
  2. Use console.log() to display outputs
  3. Some blanks (_) need to be replaced with numbers or code
  4. Important!!! Under each JavaScript hack, be sure to add an image or screenshot of your outputs in console
  5. Have fun!

🧩 Task #1

Write a for loop that prints the numbers 1 through 10.

%%js
for (let i = 1; i <= 5; i++) {
    console.log("*".repeat(i));
}

<IPython.core.display.Javascript object>

🧩 Task #2

Write a while loop that prints the even numbers between 2 and 20.

%%js
let num = 1;
while (num <= 5) {
    console.log("*".repeat(num));
    num += 1;
}

<IPython.core.display.Javascript object>

🧩 Task #3

Make the following shape using iteration loops with the symbol *:

*
**
***
****
*****
%%js
let stars = "";
for (let i = 1; i <= 5; i++) {
    stars += "*";
    console.log(stars);
}

<IPython.core.display.Javascript object>

🧩 Task #4

Use a loop to calculate the sum of all numbers from 1 to 100, and print the result.

%%js
let sum = 0;
 for (let i = 1; i <= 5; i++) {
     sum += i;
 }
 console.log("The sum is:", sum);

<IPython.core.display.Javascript object>