educative.io

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


Type your question above this line.
what is:var sums = []; an empty erray never mentioned before. var count = 0; never mentioned before.
don’t understand the sub for() function. need explanation for each row in that function.

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

Hi @masha,

Here is the code explanation:

  • var sums=[] on line 2 is an empty array which we will use to store our results. This array will be returned at the end of the function.

  • var sum=0 on line 3 is used to store the sum of subarrays which is then appended to the sums array after the whole subarray has been traversed in the second loop.

  • var count=0 on line 4 seems to have no purpose in this function. The function works fine without it.

  • Nested loops are used to traverse the arrays. The outer loop is used to traverse through the array treating the subarrays as elements of the array. The inner loop is used to traverse through each sub-array, adding each number of the subarray to sum. After the inner loop ends, sum is appended to the sums array and we move on to the next subarray. This way we get the sum of each subarray and save it to the sums array. The sums array is then returned at the end of the function.

Please do let me know if anything is still not clear. I’ll be happy to elaborate it further.

Thanks a lot dear saad.
Steel, have difficulty in understanding row 6: what is the different between numbers.length and numbers[i].length?
I see there must be twice var sum in the outer and inner function. What the efferent between them?
And row 7: need explanation.
Js is a little difficult for me. Its my first experience in coding for me, a challenge. So I guess there will be more questions later.
Again, thanks for your patience.
Have a nice day!!!