Crashing my server with this function. Please, help

Hi everyone! After studying with Codecademy for almost a year I’ve decided to take the leap and enter a Bootcamp. I have been doing really well, but now it’s time for me to come up with my own functions to solve the prompts. The problem is I have been working on it for about 40 minutes and every time I try to run the for loop everything crashes. I’m probably missing something simple, but the more frustrated I become, the less I can see anything wrong with the code :smiley: . I will leave the prompt and my answer below. I appreciate any help! Thanks in advance.

  • Whitney
    Write a function that would get the sum of all the even numbers from 1 to 1000. You may use a modulus operator for this exercise.

starter code:
function sum_even_numbers(){
var sum = 0;
return sum;
}

my code:
function sum_even_numbers(){
for ( var i = 0; i <= 1000; i + 2)
{
var sum = i

}
return sum; 

}

also here is another way that I’ve tried:

I noticed a space before var in

Did you try removing the space?

You’ve created an infinite loop because the value of i doesn’t change just by writing i + 2

Click for another hint

i += 2 would increase the value of i

Note: you will have the same issue with sum + 2

1 Like

In your first example, is there a reason you are setting sum to i with each iteration? You might consider adding a console.log(sum) in each iteration so you can see what your loop is actually doing.

Good luck!

Hi Chris,
it says to return sum not return i so I figured if i made them equal to each other when I add 2 each time to i it would do the same to sum.

Thanks I will give it another try.

Hi everyone! Thank you for your help. I was able to figure out what was wrong with my for loop and then work from there.

2 Likes