Split array method redeclared [ i ]- issue debugging

I am learning Javascript and am having issues with the split array method. I would like to see how the split function will organise my string values. I keep receiving an ‘Uncaught SyntaxError: redeclaration of let i’ message. This is the code, I am not sure what I am doing wrong:

let someArray= [
‘Bananas:6.99’,
‘Apples:3.99’, ’
Tomatoes: 30.99’ ];
for ( let i=0, i<= someArray.length, i++) {
let arrayList = someArray[i].split(’:’);
console.log(arrayList);
}

I myself am only learning JavaScript, but some of the bugs I’ve noticed are:

You use typographic quotes multiple times in your code. Tip: use ASCII machine quotes ( ’ ’ ). Here is an article on quotes on the wiki. JS is supposed to be very sensitive about this kind of thing.

for ( let i=0, i<= someArray.length, i++)
After the initial value (let i = 0) and condition (i <= someArray.length) in for loop, you need to put a semicolon:
for ( let i=0; i<= someArray.length; i++)

I’m not sure if that’s all:
js

But I haven’t gone that far yet. If you yourself do not understand what the matter is, wait for a more intelligible answer :grinning:

@Ti_awer You were on the right track with this analysis. You were only still getting a syntax error because the exit condition of the for loop is i<= someArray.length. It therefore runs the loop four times, for i values 0, 1, 2, and 3 (someArray's length is 3), and errors on let arrayList = someArray[3].split(’:’); because there is no index of 3 in that array.

This is fixed by changing the exit condition to i < someArray.length.

So the final working code is:

let someArray = ['Bananas:6.99','Apples:3.99','Tomatoes: 30.99' ];

for (let i = 0; i < someArray.length; i++) {
  let arrayList = someArray[i].split(':');
  console.log(arrayList);
}

BTW, @you_N_me probably didn’t put the typographic quotes in — they are inserted automatically by discourse in normal text, which is annoying. You can stop this by putting your code snippets and blocks in code using backticks/code fences (markdown syntax).

Do I understand correctly that because of i <= someArray.length the for loop will loop through four circles instead of three? The first (in which the value i = 0) logs Bananas, the second (i = 1) logs Apples, the third (i = 2) logs Tomatoes, but the fourth (i = 3, which won’t happen when i < someArray.length) doesn’t exist and it creates mistake?

Thanks for the explanation.

@Ti_awer Yes, you are basically correct. someArray.length is equal to 3. But the loop is starting its iteration with i = 0, so we get four iterations, with the following code run:

let arrayList = someArray[0].split(’:’);
let arrayList = someArray[1].split(’:’);
let arrayList = someArray[2].split(’:’);
let arrayList = someArray[3].split(’:’);

The fourth line throws an error because someArray[3] doesn’t exist.