Basic JavaScript: Iterate Through an Array with a For Loop

Basic JavaScript: Iterate Through an Array with a For Loop
0

I solved this, but at first time I tryed thi way and don’t get well why don’t work.
Why total =+ i dont return 20?

Your code so far


// Setup
var myArr = [ 2, 3, 4, 5, 6];

// Only change code below this line
let total = 0;
for(let i = 0; i < myArr.length; i++){
total += i
}

console.log(total)

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

total =+ i is the same as total = +i, you are just using the assignment operator and assigning the value of i to total
instead total += i is equal to total = total + i

and also i is the index, not the number inside the array, so it doesn’t work the same

Remember, you want to sum the contents of the array. If your som isn’t referencing the array in some way, that’s probably a bad sign.