Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 219 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrong answer to "FizzBuzz" style code

#1
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]:

[To see links please register here]

Reply

#2
I don't think anything is wrong with your code - when I run it I get 233168 which seems right.

As for indenting your code, you have to use spaces. I usually indent in my text editor and then copy and paste in.

Finally for the code snippets I don't have much experience with them but I believe you need to log your answer to the console, not just return it.

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);
}
} //This is the main loop
var sum = array.reduce(function(a,b) {
sum = a+b;
return a+b;
}); //array reduce function to summate the array.
console.log(sum);
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through