educative.io

Educative

https://www.educative.io/courses/learn-html-css-javascript-from-scratch/gxZAOwDB7pZ#Exercise- more quetions

Steel, have difficulty in understanding row 6, 7, 8, 9: what is the different between numbers.length and numbers[i].length? what is: numbers[i][j]} on row 7? sums[i] on row 8? and sum = 0 on row 9.

Type your question above this line.

Course: https://www.educative.io/collection/10370001/5686791109607424
Lesson: https://www.educative.io/collection/page/10370001/5686791109607424/5631943370604544

Hey Masha!

  • numbers.length is the length of the array.

  • numbers[i].length is the length of the ith sub-array.

  • On row 6

for (j = 0; j< numbers[i].length;j++){

We iterate the whole sub-array using sub-array’s length property numbers[i].length.

  • On row 7

sum = sum + numbers[i][j]

We add each element of sub-array in each iteration. If we talk about the i=0 then in the first iteration, number[0][0] will be added. In second, number[0][1] will be added.In third, number[0][2] will be added. In fourth, number[0][3] will be added.
After four iterations, we will get out of the loop as for i=0 the numbers[i].length is four, and the sum will have the sum of all elements of ith sub-array.

  • On row 8

sums[i] = sum;

We store the calculated sum of all elements of ith sub-array on the ith position of a new array sums that we have declared earlier.

  • On row 9

sum = 0;

We reset the sum variable to use it for the next ith iteration (the next sub-array).

Thank you for asking the question.