Would you please help me to understand or link or in code coach to get codes in details? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Would you please help me to understand or link or in code coach to get codes in details?

/*function main() {*/ //take the number of passed levels var levels = 3;/*parseInt(readLine(),10);*/ var points = new Array(); var count = 0; while(count<levels){ var elem = 3;/*parseInt(readLine(),10);*/ points[count] = elem; count++; /* }*/ var sum = 0; //calculate the sum of points sum = points.reduce(function(a,b){ return a+b; },0) //output console.log(sum); } /* The player receives points after passing each level of a game. The program given takes the number of passed levels as input, followed by the points gained for each level, and creates the corresponding array of points. Complete the program to calculate and output to the console the sum of all gained points. Sample Input 3 1 4 8 Sample Output 13 Explanation The first input represents the number of passed levels, -- in this case, 3 (the size of an array to be created). The next 3 inputs are the points awarded to the player

28th Nov 2022, 3:33 PM
Sony
Sony - avatar
4 Answers
+ 1
Alright, well you see the input part, there are 4 numbers, where ... First number represents number of levels a player had passed. Successive numbers represents the scores, to be accumulated Taking the task Description ... 3 // number of levels 1 // score on 1st level 4 // score on 2nd level 8 // score on 3rd level Number of scores to be read in will depend on fhe first input (number of levels passed) Our task is tio accumulate the scores, in this case 1 + 4 + 8 which results in 13 And if it was the Array::reduce() that you wished to get to know better, then you can read more about it here ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
29th Nov 2022, 7:28 AM
Ipang
+ 2
Why you commented all lines that reads user input? isn't that part of the task? Here I see you hardcode the numbers, while the code should instead be reading user input and use them. Wdym "get codes in details"?
28th Nov 2022, 3:43 PM
Ipang
+ 1
Thank you for your answer.. well I mentioned all the part of the question because to get easy to catch up the problem. I'm still unclear. I just would like to know how to figure out the number 13 as result? I just tried all of the parts what I've knowledge about this array problem accordingly. Would you please tell me? Thank you again..
29th Nov 2022, 5:35 AM
Sony
Sony - avatar
+ 1
Thank you so much.
29th Nov 2022, 8:22 AM
Sony
Sony - avatar