Basic JavaScript - Iterate Through an Array with a For Loop

Tell us what’s happening:
Describe your issue in detail here.

Your code so far
why this code can’t pass? i try to console it and its total is 20. whats wrong with this?

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 11; RMX2180) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.134 Mobile Safari/537.36 OPR/70.3.3653.66287

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

Link to the challenge:

Your loop iteration statement is wrong.

This is not adding what you think it is.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

2 Likes

change this with
Mod edit: solution removed

variable i has to be incremented as i++ so that it can move to array’s next element.
“total+=myArr[i]” adds array’s element to variable total as i iterates

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Hi @nov_Ice ,
There are 2 problems with your code.

  1. In a for loop you have to define three things.
  • starting condition
  • end condition
  • what happen in each iteration for the index

These should be in order. In your code,

  • starting condition → i = 0;
  • end condtion → i < myArr.length ;
  • what happen in each iteration to index → i=myArr.length

So intially i = 0 . But after an iteration your i=5 and your end condition is checking whether i < myArr.length(which is 5). So now, since i=5 loop will stop. So, there is a problem with your 3rd condition in the for loop.

  1. In each iteration, you are doing total = myArr[i] + [i];. Now what is the value of i? :upside_down_face:. What you need is to add each and every number in the array.

Hope you got it!! Ask if you need further clarifications :grinning:

thanks for helping me😊

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