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); }
10 Antworten
+ 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.
+ 2
no something like this sum += points[i]. sum should not be an array.
+ 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);
}
+ 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].
+ 1
So maybe a for loop and adding i to index number shall work right?
+ 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);
}
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));
}
0
I tried it but didn't worked, you mean slmething like this right, sum[i] = points[i]
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); }
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);
}