Problem 1: Multiples of 3 and 5

Tell us what’s happening:

Your code so far

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;


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.

Link to the challenge:

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)

hope this helps.

1 Like

Try moving

var sum = 0;

inside the multipliesOf3and5 function. I think this global variable is messing up the tests.

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 is for Project Euler, right?

If so, you’ve gone about this the “wrong” way

Project Euler problems are designed to have a trick to solving them more efficiently

I’ll give you a clue for this one:

3 + 6 + 9 + ... = 3 * (1 + 2 + 3 + ...)

Similarly for 5s case. See if you can see how to use this fact to solve the problems in constant time


Thank you for your help,but I already solve it after posting it in forum.

Thanks for your help but I already solve it after posting.

It’s worth redoing it in a more efficient way

In a sense it’s the point of PE questions

You can reduce your O(n) solution down to O(1)! That’s worth doing!

Thanks… This is pretty helpful to reduce complexity.

104 characters long version:

const sum = n => {
  for(var s = 0, i = 1; i <= n; i ++) {
    !(i % 3 && i % 5) && (s += i)
  return s;

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;

how does it know to count numbers that have multiples of BOTH 3 and 5 only once??

in all versions in this thread all the functions has just a command making the sum

so you have

if (/* check if the number is divisible by 3 or 5 or both */) {
   // above true add number to sum

all the other ways are variants that still work in this way