need help understanding for...of loop

67 Views Asked by At

My teacher wanted me to practice for...of loops this is the example. I've provided you with an array called numbers. print out the square of each value (the number multiplied by itself). const numbers = [1,2,3,4,5,6,7,8,9];
Your output should look like this:
1 4 9 16 25 36 49 64 81; I looked for...of on MDN i saw you can add by doing (let num of sum); { num += 1; console.log(num); } so i tried num *=2 but that will make 1 a 2 so I tried num *= num; . his answer came back as for (let num of numbers) { console.log(num * num);} . I'm guessing mines is wrong because I *num; and that's NAN, can you explain why num*num is in the console.log();

edit: I think people are misunderstanding what i wrote i wrote

for(let num of numbers){ num *= num; console.log(num)

1

There are 1 best solutions below

1
On

Both of these writing methods are effective

    const numbers = [1,2,3,4,5,6,7,8,9];
    for(let num of numbers){
      console.log(num*num)
    }

    const numbers = [1,2,3,4,5,6,7,8,9];
    for(let num of numbers){
      console.log(num**2)
    }