Beginner Challenge: Sum all even numbers 1-1000. Is my rationale wrong or just my code?
<p>I just began coding the other day and I'm going through coding dojo's free beginner algorithm course. I had no trouble with the intro lesson predicting outputs, but now I am writing code my thoughts don't seem to translate well. </p>

<p>The task is to write a function that would get the sum of all the even numbers from 1 to 1000. They mentioned I could use the modulus operator, but I think that operator is a little contrived. Instead I figured it easier and more efficient to run a For loop, starting at 0 and adding 2 each loop. After each loop the new number would be added to the previous which would be the current sum variable. I have attached my attempted solution:</p>

<pre><code>function sum_even_numbers(){
    var sum = 0;
    for(var x=2; x!=1000; x+2)
    {
      sum+=x;
    }
    return sum; 
}
</code></pre>

<p>Unfortunately this only freezes my browser until I stop script (I guess it's on an infinite loop). Most of the previous lesson work involved outputs using console.log() and I'm not even sure why I can't use it instead of the return operator. Can tell me if my way of going about solving the problem is valid or not and if it is, what's wrong with the code. And if it's not, where the flaw is in my logic. I have included the suggested solution below.</p>

<pre><code>function sum_even_numbers(){

    var sum = 0;
    for(var x = 1; x &lt; 1001; x++){
      if(x % 2 === 0){
      sum += x;
      }
    }
    return sum; 
}
</code></pre>

<p>Thanks all</p>

<p>Also if you have time, why does a semicolon after the If operator change the answer so much? I still don't know the hard rules of semicolon use.</p>

<p>Edit: Thanks guys, what a great community!</p>