Active learning with Array's

I’m working on the active learning section of Array’s - the first exercise to total the receipts.

This is what I had for the ‘split’ portion. I checked the solution and I don’t know why the [i] is required or what it is? Thanks for your help!

products.split(’:’);

@Tess_Bishop ah, so you are talking about this line:

let subArray = products[i].split(':');

?

The [i] is there because the products object we are working with contains an array. To access each item in the array, we use bracket notation — [0], [1], etc.

In this case we are using a for loop to access each array item in turn, split the string that it contains, display it inside the list, add the value to the total count, etc.

So the first time the loop runs, let subArray = products[i].split(':'); is equal to let subArray = products[0].split(':');, then it is equal to let subArray = products[1].split(':');, etc., right up to when it equals let subArray = products[products.length].split(':');. Then it stops.

You can read up on loops here: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code.

1 Like