Project Euler #1: Multiples of 3 and 5

Hey all, I’m working on the Project Euler section using vanilla JS, however I’m getting stuck on the first one. This looks really similar to fizzbuzz to me so I used the same basic idea (calculate whether there is a remainder for a given number divided by 3 or 5 but add it to a variable countSum instead of printing it to the console or an array. The code that I have “works” in the sense that the value of countSum is correctly calculated as displayed by the console, however the code I have isn’t getting accepted. I tried shifting the return statement at the end of the function around, which doesn’t seem to do anything. Help?

Link to the Challenge

var countSum = 0;

function multiplesOf3and5(number) {
  for (let count = 3; count<number; count ++)
    if(count % 3 == 0 || count % 5 == 0) {
      countSum += count;
    return countSum;


Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.


var myGlobal = [1];
function returnGlobal(arg) {
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

That fixed it, thanks! I moved the declaration right above the for loop and changed it to let. If I understand correctly, doing it this way means that every time I call the function, countSum gets reset to zero before the loop starts using whatever number I pass to it, right?

a new countSum variable that is local to the function is declared each time. It is not the same on that is reset to 0.