How do I sum up the value in the arrays? (for my code project) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I sum up the value in the arrays? (for my code project)

For example the input: 2 4 3 the first value represents the 'level', also the size of the array to be created, the next 2 inputs is the points they gained for passing 2 levels... how to sum up them and become 7??

8th Sep 2023, 11:15 PM
Mex
Mex - avatar
2 Answers
+ 2
So first value is input and it shows number of levels? Then you have an array whose length is number of levels? Then you get 4 points for passing level 1 and 3 points for passing level 2? so you have let arrayLevel=[]; now you can add your level points to array. arrayLevel.push(4); arrayLevel.push(3); Then create variable for sum let sum=0; And then you iterate through array (fet i=0;i<arrayLevel.length;i++){ sum+=arrayLevel[i]; } console.log(sum);
8th Sep 2023, 11:55 PM
Samir Krasnic
Samir Krasnic - avatar
+ 2
Thank you so much for the reply, it works :)
9th Sep 2023, 4:26 AM
Mex
Mex - avatar