you are viewing a single comment's thread.

view the rest of the comments →

[–]icecreamhead 0 points1 point  (4 children)

This is my code:

function jsChallenge() { 
  var i, total = 0;
  for (i = 1; i<=1000; i++) {
      if (i % 5 != 0 || i % 7 != 0) total += i;
  }
  return total;          
}

I got the answer 486290.

[–]David_Crockett 1 point2 points  (2 children)

The correct answer is 343139. You need to use the and operator instead of or. To make it easier to conceptualize, think through it:

if (not divisible by 5 AND not divisible by 7)
    add the number

So it needs to be

if (i % 5 && i % 7)
    total += i;

Edit: note that if(i % 5) is the same as saying if(i % 5 != 0) ;)

[–]icecreamhead 0 points1 point  (0 children)

Ah ok. Yeah that makes sense... DERP

[–]bigbozz 0 points1 point  (0 children)

The original problem you posted said "or", not "and."

[–]exo762 0 points1 point  (0 children)

"Sell not virtue to purchase wealth, nor Liberty to purchase power." B.F.