Basic JavaScript - Iterate Through an Array with a For Loop

Tell us what’s happening:
Describe your issue in detail here.
I can’t get * total should equal 20. and You should use a for loop to iterate through myArr.
Your code so far

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

// Only change code below this line
for (let total = 0; total >= myArr.length; total--) {

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Iterate Through an Array with a For Loop

Link to the challenge:

hi, for-loop might be crazy at beginning. You can refer to this : DevTips Daily: Sum an array of numbers in JavaScript - YouTube

you set total to 0
myArr.length = 5
The loop is set to run while total >= myArr.length; ie while 0 >= 5, which is never, so your loop will never run

Follow the instruction:

  1. “Declare and initialize a variable total to 0
variable = starting number (guidance)

" Use a for loop to add the value of each element of the myArr array to total":
2. In ‘for’ loop declare and use “i” variable, not “total”.
3. If i is declared and initialized with 0, and i is less than the length of the array, increase the i by 1, until i is not equal to the length of the array
4. Now, with each iteration through the myArr, your ‘total’ variable should be increased by the next number from the array, :

variable += myArr[i];    (this is guidance)
  • for i=0, variable = 0 + first number from the myArr,
  • for i=1, variable = 2 + second number from the myArr,
  • for i=2, variable = 5 + third number from the myArr,
  • for i=3, variable = 9 + fourth number from the myArr,
  • for i=4, variable = 14 + fifth number from the myArr,
  • for i=5 (which is the length of the myArr), variable = 20.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.