0

Array Properties And Methods Challenge

Can someone please help me with this challenge. Test case 4 and 5 work but 1,2 and 3 do not. 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 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 + points.length + elem + count; //calculate the sum of points //output console.log(sum); }

22nd Nov 2020, 9:50 AM
Brandon Lee Banks
Brandon Lee Banks - avatar
34 ответов
+ 6
What's readLine()? No need to add points.length and count, only add elements of the array.
22nd Nov 2020, 10:01 AM
Gordon
Gordon - avatar
+ 12
Hi This is my programming and it worked very well 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 = points[0]; //calculate the sum of points for (i=1;i< points.length ;i++){ sum = sum + points[i]; } //output console.log(sum); }
2nd Jan 2021, 3:17 PM
Taymaa
+ 5
Update on the problem. I managed to get all test cases to work, thank you to everyone who commented... here is the solution. 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 = points.reduce(function(a, b) { return a + b; }, 0); //calculate the sum of points //output console.log(sum); }
22nd Nov 2020, 3:03 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
+ 3
Gordon readline() is reading stdin in Node.js
22nd Nov 2020, 1:29 PM
Solo
Solo - avatar
+ 2
This worked for me 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 = sum + points[i]; } console.log(sum); }
12th Jul 2022, 11:15 AM
Allan Nyabuti
+ 2
What does .reduce mean? We haven't had a lesson on that, yet it is used in the sample solution.
2nd Oct 2022, 7:50 AM
Kerissa Poss
Kerissa Poss - avatar
+ 1
var sum = points[0]; //calculate the sum of points for(i=1; i<points.length; i++){ sum += points[i]; } //output console.log(sum);
19th Jun 2021, 9:59 AM
Agon Istrefi
+ 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++; } //calculate the sum of points var sum = 0; for(let i=0;i<points.length;i++) sum+=points[i]; //output console.log(sum); }
12th Oct 2021, 5:33 PM
Hasan Ali
Hasan Ali - avatar
+ 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; for (i=0;i< points.length ;i++){ sum = sum + points[i]; } console.log(sum); }
20th Sep 2022, 4:11 PM
Dilan Tharaka
Dilan Tharaka - avatar
+ 1
Here is the correct code 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 = points.reduce(function(a, b) { return a + b; }, 0); //output console.log(sum); }
31st Mar 2024, 9:01 AM
Ngoe Herbert Molio
Ngoe Herbert Molio - avatar
0
Gordon, readLine is the input that will be used to test your code. So how would I do that if the numbers are changing for each case?
22nd Nov 2020, 10:06 AM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
Challenge Declined!
22nd Nov 2020, 10:19 AM
Muhammad Atif Waheed
Muhammad Atif Waheed - avatar
0
function main() { //take the number of passed levels var levels = parseInt(readLine(),10); var points = new Array(); var count = 0; var sum = 0; while(count<levels){ var elem = parseInt(readLine(),10); points[count] = elem; //calculate the sum of points sum += points[count]; count++; } //output console.log(sum); }
6th Dec 2021, 4:24 PM
Peter Coker
Peter Coker - avatar
0
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++; } if (count == 4 ) { var sum = points[0] + points[1] + points[2] + points[3]; } if (count == 2 ) { var sum = points[0] + points [1]; } if (count == 3) { var sum = points[0] + points[1] + points[2]; } if (count == 5 ) { var sum = points[0] + points[1] + points[2] + points[3] + points[4]; } //calculate the sum of points //output console.log(sum); }
10th Dec 2021, 2:33 PM
Vu Ba Luc
Vu Ba Luc - avatar
0
function main() { //take the number of passed levels var levels = parseInt(readLine(),10); var points = new Array(); var count = 0; var sum = 0; while(count<levels){ var elem = parseInt(readLine(),10); points[count] = elem; sum = elem + sum count++; } //output console.log(sum); }
13th Jan 2022, 10:21 AM
Eyal R
0
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 = sum + points[i] } //output console.log(sum); }
20th Jan 2022, 4:00 PM
Nima Zakariyaee Koochaksaraee
Nima Zakariyaee Koochaksaraee - avatar
0
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 = points.reduce(function(a, b) { return a + b; }, 0); //calculate the sum of points //output console.log(sum); } Good Luck
25th Jan 2022, 8:34 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
The answer is. please copy the whole code and paste it 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 = points[0]; //calculate the sum of points for (i=1;i< points.length ;i++){ sum = sum + points[i]; } //output console.log(sum); }
26th Apr 2022, 9:02 AM
Saman Chowdhury
0
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 = points[0]; //calculate the sum of points for (i=1;i< points.length ;i++){ sum += points[i]; } //output console.log(sum); }
6th Sep 2022, 1:04 PM
Alexander OKUNRINKOYA
0
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(x = 0; x < points.length; x++){ sum += points[x]; } //output console.log(sum); }
27th Sep 2022, 12:27 PM
Hashim Nuruddeen Abubakar
Hashim Nuruddeen Abubakar - avatar