Increment a Number with JavaScript ++

Increment a Number with JavaScript ++
0

#1

Tell us what’s happening:

The test of changing the code is not running (only that one). and obviously i’ve changed it!

Your code so far

myVar = ++myVar ;


var myVar = 87;

// Only change code below this line
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.77 Safari/537.36.

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


#2

Read the instructions again carefully. Look at the example code and read the “Note”.


#3

Hello,

your syntax is incorrect. Try reading the note in the exercise again.


#4

Technically your code should pass all of the tests given what they say explicitly. Say you have a variable like so:

var a = 0;
a++;
a === 1; //true

it is the same as:

var a = 0;
a = a + 1;
a === 1; //true

what you did was:

var a = 0;
a = ++a;
a === 1; //true

++a evaluates to a + 1 so assigning a = ++a; will do the same thing as a++; or ++a.
if you instead do a = a++ a will be assigned the current value of a (or NaN if a is not a number) and the increment won’t happen.

The point of this challenge is to teach you the shorthand of incrementing a number by one like this though:

a++;
// a is now 1 more than before

This syntax is very useful and used in many programming languages.


#5

Thanks guys! I understand it now