Getting the value of each nested array

Hi,

I hope someone will be able to help with my predicament. I am writing a program for an exam that is due tommorrow and I can not for the life of me figure out how to get the sum of nested arrays.

if I have an array like this:

myArr = [[120, 80, 90], [120, 90, 70], [130, 90, 55]];

then I want the first index 0 to be added together so a total of 864000.

So all the nested arrays should be added:
myArr[0] = 864000
myArr[1] = 756000
myArr[2] = 643500

I sincerely hope someone can help.

You will need nested loops.

The first loop will go through each array in myArr, then when the loop goes through each array, you can make another loop that will go through each value in each array and keep adding on to the sum.

Thank you for the fast response :slight_smile:

I will try and loop through it.

I will bedste back to tell how I bested this wreched issue.

Hi shimphillip,

yup the iteration worked like a charm. There may be a more concise way of doing it using .reduce maybe, but this works.

var samlet = 0;
for (var i = 0; i < 5; i++) {
samletIteration = collieArr[i][0] * collieArr[i][1] * collieArr[i][2];
samlet += samletIteration

1 Like

At 2:19 he covers this very clear and consise.

Thank you VictorVermeulen, i will check it out :slight_smile:

No problem. I recommend watching the entire video and making notes if you need a refresher. I did this in a couple of days after a long break (too long) and feel like the challenges are pretty easy now!

1 Like