Array Properties & Methods Changllenge
The chanllenge is: 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 point. Can someone help me with why the first solution is working but the second one is not working? I suppose they are similar. Solution 1: function main() { //take the number of passed levels var levels = parseInt(readLine(),10); var points = new Array(); var count = 0; while(count<levels){ var elem = parseInt(readLine(),10); points[count] = elem; count++; } var sum = 0; //calculate the sum of points for (i=0; i<points.length; i++) { sum+=points[i]; } //output console.log(sum); } Solution 2: function main() { //take the number of passed levels var levels = parseInt(readLine(),10); var points = new Array(); var count = 0; while(count<levels){ var elem = parseInt(readLine(),10); points[count] = elem; count++; } var sum = 0; //calculate the sum of points var n = points[0]; while (n>0) { sum+=points[n]; n--; } //output console.log(sum); }