var sum = 0;
function multiplesOf3and5(number) {
// Good luck!
for(var i = 1; i < number; i++){
if((i % 3 === 0 )||(i % 5 === 0)||(i % 3 === 0 && i % 5 === 0)){
sum = sum + i;
}
}
return sum;
}
multiplesOf3and5(1000);
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/66.0.3359.181 Chrome/66.0.3359.181 Safari/537.36.
first thought: why do you start from i=1 ? The function is called multiples of 3 and 5, therefore it seems logical to start from 3?
Multiples of three { 3, 6, 9…}
Multiples of five {5, 10, 15…}
Also you’ve defined a global variable which seems like overkill. Move the sum variable down into the function scope.
So then you can use console.log to print out the value of sum as you go and change 1000 to something like 20 so it is a manageable set of results for you to debug.
(open the console window for your browser to see what you are logging and then determine any further action based on what your log statement gives you)
I should also point out that for this if statement, the last term of your if statement is not needed. That is because when we use logical OR , the first term to give true shortcuts the if statement. So if the number 15 for eg is a multiple of 3, it will return after the first term returns true right away. If the number is not a multiple (say 10), then it will return after the second term right away. So the extra check is not needed.
This solves all the problems
function solution(number){
let sum = 0;
for (let i = 3; i <number; i++) {
if ((i % 3 === 0) || (i % 5 === 0)){
sum += i;
}
}
return sum;
}