Problem 1: Multiples of 3 and 5 Why my function returns false result for any number besides 10?

Tell us what’s happening:

Look at console logs, you’ll see that the array is supposed to be right since it pushes numbers under the argument"number"

``````
function multiplesOf3and5(number) {
var sum = [];
var multi = 0;
for(var i = 0; multi*3 < number;multi++) {
i = multi*3;
sum.push(i);
}
console.log(sum);
multi = 0;
for(var j = 0; multi*5 < number;multi++) {
j = multi*5;
sum.push(j);
}
console.log(sum);
return sum.reduce((a,b) => a + b);
}

multiplesOf3and5(49);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0`.

In your example call above multiiplesOf3and5(49), you are double counting some numbers. For example, you are counting 15, 30, and 45 twice.

1 Like

Yeah, that was because there were some numbers divisible(x % y = 0) by 5 or 3. Now I added an if condition to prevent that from happening:

``````function multiplesOf3and5(number) {
var sum = [];
var multi = 0;
for(var i = 0; multi*3 < number;multi++) {
sum.push(multi*3);
}
multi = 0;
for(var j = 0; multi*5 < number;multi++) {
if (multi*5 % 3 > 0) {
sum.push(multi*5);
}
}
return sum.reduce((a,b) => a + b,0);
}

multiplesOf3and5(10);
``````

Thank you for the help!

I have been playing more with the code and seen a much better solution:

``````function multiplesOf3and5(number) {
var sum = 0; // sum for multiples of 3 and 5;
for(var i = 0; i < number;i++) { // a loop to increase i everytime
if (i % 3 === 0 || i % 5 === 0) { //this condition is important because everytime i increases, this condition checks if it's a multiple(divisible by) for either 3 or 5.
sum += i; //increases sum with i that is divisible with 3 and 5
}
}
return sum; //returns sum of numbers below "number" that are multiples of 3 and 5
}

multiplesOf3and5(10);
``````

It’s shorter and cleaner.