What´s wrong in my Increment a Number with JavaScript

What´s wrong?

Your code so far

var myVar = 87;

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:
https://www.freecodecamp.org/challenges/increment-a-number-with-javascript

Read the clue again. :slight_smile:

The entire line becomes i++;, eliminating the need for the equal sign.

But I have done that already and says it´s wrong either
var myVar = 87;

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

It says:
myVar should equal 88
myVar = myVar should be changed

you started with myVar being equal to 87
then you added 1. So now myVar is equal to 88.
Then you re-assigned 88 again to myVar.
Then you increment myVar by 1.
What is now the value of myVar?

89

Why?

You have an extra increment in your code.

I do not understand really what I am doing, I need more explanations. What´s the point of doing this? I mean, am I doing an arithmetical sum or just telling what it should do, sum 1 + var?

The point is to gain an understanding of the different ways of incrementing a number. The fact that you’re having difficulty with is why you’re doing it. Because this is pretty basic and things you need to understand when progressing.

Yes, I understand now I thought there were some more mistery! Now I am struggling to understand the difference between ++myVar and myVar++, for me looks the same

I have got it already. Thanks

The difference is that ++myVar will return the incremented value of myVar while myVar++ will return the old value of myVar. So writing myVar = ++myVar (though completely pointless other than as an example) will increment myVar by 1 then return the value of myVar. Writing myVar = myVar++ will increment the value of myVar, return the old value and set myVar back to the old value. In this case it’s just to demonstrate how they work, but it can be useful in many cases.

2 Likes