freeCodeCamp Challenge Guide: Increment a Number with JavaScript

Increment a Number with JavaScript


Hints

Hint 1

You can easily increment (add one) to a number variable by using the ‘++’ increment operator. For example:

var a = 6;
a++; // Now, 'a' is equal to 7 -- post-fixing
++a; // Now, 'a' is equal to 8 -- pre-fixing
4 Likes

Why isn’t this working?
"var myVar = 87;

// Only change code below this line
myVar = ++myVar;"
it says myVar = myVar should be changed but it is changed it says ++myVar now, so what exactly needs to be different?

6 Likes

Oh jeez it was even easier than I thought if anyone is struggling the answer is just
"++myVar" no myVar = or anything.

39 Likes

Thankyou My fellow camper. I was stuck on this one although I knew I needed to erase the equal sign I didn't realize I needed to delete eveyrthing except myVar then add ++ in front myVar I didnt realize that i represented or is the same thing as myVar.

P.S. dont forget the semicolin guys and gals
Happy codeing.

4 Likes

var myVar = 87;

// Only change code below this line
++myVar;

10 Likes

Thanks - I was struggling with this one, too!

1 Like

I see that this answer can work 2 ways. I feel like the answer should be:

myVar++;

because the “i” in the increment myVar, there for it should go first. Both ways work. I just fear that the order of things will matter more later. ALSO i got this wrong the first time because I didn’t use CamelCase :tired_face:

6 Likes

Is this incorrect?

var myVar = 87;

// Only change code below this line
myVar = 86 + 1;
myVar++;

5 Likes