Array Properties & Methods Challenge ** Real Solution | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Array Properties & Methods Challenge ** Real Solution

I very close to solve this without cheating, can somebody try to explain to me this? 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 sum = new Array(points.length); sum[0] = points[0].length; sum[1] = points[1]; sum[2] = points[2]; sum[3] = points[3]; sum = points[0] + points[1] + points[2] + points[3]; //output console.log(sum); }

8th Jan 2021, 8:40 PM
Marcos Treviño Rodriguez
Marcos Treviño Rodriguez - avatar
10 Answers
+ 3
This seems unnecessarily complicated. You should explore the idea of the for loop with i for the index a little further. The solution can be very straightforward with only one variable for the sum needed.
10th Jan 2021, 2:51 PM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 2
no something like this sum += points[i]. sum should not be an array.
10th Jan 2021, 3:13 PM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 2
Omg it worked actually better this way and i didn't have to use some weird formula that i didnt know. Thanks alot 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); }
10th Jan 2021, 6:17 PM
Marcos Treviño Rodriguez
Marcos Treviño Rodriguez - avatar
+ 1
You cannot assume that the array is always going to be 3 elements. That is why they are passing the number of levels first and then constructing the array from that. You need to make the summation generic to accommodate any number of array elements. Consider using a loop to assist with this and remember that you do not have to create unique variables for each value and then sum them up individually. Instead you can have one variable that can be increased by each value of the array, such as: sum += points[index].
10th Jan 2021, 6:05 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 1
So maybe a for loop and adding i to index number shall work right?
10th Jan 2021, 8:33 AM
Marcos Treviño Rodriguez
Marcos Treviño Rodriguez - avatar
+ 1
Hello, this is my solution. If you have any questions pls ask. 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(x = 1;i < points.length;x++){ sum += points[x]; } //output console.log(sum); }
15th Dec 2021, 5:03 PM
Mircea Teodor Cofa
Mircea Teodor Cofa - avatar
0
This was my final 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 = 0; var reducer = (a, b) => a + b; //calculate the sum of points var sum = new Array(points.length); sum = points; //output console.log(sum.reduce(reducer)); }
10th Jan 2021, 8:36 AM
Marcos Treviño Rodriguez
Marcos Treviño Rodriguez - avatar
0
I tried it but didn't worked, you mean slmething like this right, sum[i] = points[i]
10th Jan 2021, 2:54 PM
Marcos Treviño Rodriguez
Marcos Treviño Rodriguez - 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 = 0; //calculate the sum of points for(i = 0; i < points.length; i++){ sum += points[i]; } //output console.log(sum); }
24th Sep 2021, 10:02 AM
Elhouari Mohamed
Elhouari Mohamed - avatar
0
very easy and lazy vay :)) function main() { //take the number of passed levels var levels = parseInt(readLine(),10); var points = new Array(); var sum = 0; var count = 0; while(count<levels){ var elem = parseInt(readLine(),10); points[count] = elem; count++; sum += elem } //output console.log(sum); }
8th Feb 2022, 10:42 AM
Halil İbrahim SOYMAN
Halil İbrahim SOYMAN - avatar