Increment a Number with JavaScript don't understand

Tell us what’s happening:

Your code so far


var myVar = 87;

// Only change code below this line

myVar = myVar++;
myVar++;

//

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/increment-a-number-with-javascript

Hi,

It isn’t necessary to write MyVar = myVar++;

You can just write myVar++;

That’s because myVar was previously initialized with “var”

Hope this helps.

1 Like

oh thank you very much!!!

So the first line you’ve left in there, myVar = myVar++; will assign myVar to the value of myVar and ignore the ++. Instead, get rid of that line. The second line is all the test is looking for.

A simple way to see how this works, open your console (in the “developer tools” menu on most browsers), and type the following javascript directly into the console, typing a return at each line. This is actually executing the javascript as you go, line by line:

var myVar = 12       // sets the value of myVar
myVar                // shows you the current value
myVar = myVar++      // let's try this first
myVar                // again, show the current value of mVar
myVar++              // now lets try the increment operator
myVar                // and one more time, let's see the current value

Typing directly into the console is a handy way of testing simple javascript.

1 Like