07-24-2023, 06:59 AM
The original question is:
What is the sum of all multiples of 3 and 5 from 1 to 1000?
I wrote this code to solve that.
<!-- begin snippet: js hide: true -->
<!-- language: lang-js -->
var array =[]; //Setting up the Array
for (i=0 ; i<1000 ; i++) {
if(i %3 === 0) {
array.push(i);
} else if (i%5 === 0) {
array.push(i);
} else{
};
}; //This is the main loop
var sum = array.reduce(function(a,b) {
return a+b;
}); //array reduce function to summate the array.
<!-- end snippet -->
Now This does return 467, but that does not seem to be the correct answer as you can see [here][1]
Also, I am new here and would like to know how the code snippet works, because i am not getting a return value at all.
Also, how do I indent my code here? tab doesn't work.
[1]:
What is the sum of all multiples of 3 and 5 from 1 to 1000?
I wrote this code to solve that.
<!-- begin snippet: js hide: true -->
<!-- language: lang-js -->
var array =[]; //Setting up the Array
for (i=0 ; i<1000 ; i++) {
if(i %3 === 0) {
array.push(i);
} else if (i%5 === 0) {
array.push(i);
} else{
};
}; //This is the main loop
var sum = array.reduce(function(a,b) {
return a+b;
}); //array reduce function to summate the array.
<!-- end snippet -->
Now This does return 467, but that does not seem to be the correct answer as you can see [here][1]
Also, I am new here and would like to know how the code snippet works, because i am not getting a return value at all.
Also, how do I indent my code here? tab doesn't work.
[1]:
[To see links please register here]