Increment a Number with JavaScript1

Increment a Number with JavaScript1
0

#1

Tell us what’s happening:

Your code so far


var myVar = 87;

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

Your browser information:

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

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


#2

While ++myVar also increments, for some reason only 'myVar++` passes the test.


#3

but still trowing error


#4

You must use myVar++ and must include ; at the end, which is pretty silly. Note that this is just for this challenge. I’d prefer to use ++myVar


#5

Yeah, I’m getting the same error. Let me check more.


#6

var myVar = 87;

// Only change code below this line

myVar = myVar++ ;

test result
// running test
myVar should equal 88
myVar = myVar + 1; should be changed
// tests completed

second case:
var myVar = 87;

// Only change code below this line

myVar = ++myVar ;

result:
// running test
myVar = myVar + 1; should be changed
// tests completed


#7
myVar++;

implies assignment.

You don’t really need to assign it again. I think the challenge checks for a specific string or something.


#8

so what should be the correct answer


#9

The code above, which I posted.


#10

thanks
finally its work


#11

Ah, I found it, I was leaving off the terminal semicolon.


#12

I’m not sure what the tests are checking for under the hood to explain why myVar++ is working and ++myVar is not, but I just wanted to post quickly an example of the difference between the two:

++myVar is incrementing the variable and returning the new number. myVar++ returns the number before it was incrementing. Doing console.log(myVar) after the increments will log the same thing no matter which you use, so I have a feeling the code running the tests is checking what’s getting logged when the increment is happening in addition to the new value of myVar.

For example:

var num = 0;

if (num++ === 1) {
  console.log('Num now equals 1'); // not going to get logged
}


// alternatively 
var num = 0;

if (++num === 1) {
  console.log('Num now equals 1'); // this is going to get logged
}


#13

Just know that

myVar = myVar + 1;
myVar += 1;
++myVar;
myVar++;

will have the same effect.

Also, note that the last form evaluates slightly later than the first three. So, always stick to the first three forms unless you know what you are doing.

For example, run this code and check the results:

var myVar1 = 0
var myVar2 = 0

console.log(++myVar1)
console.log(myVar2++)

console.log(myVar1, myVar2)

#14

This is one test that allows either:

{
  "text": "Use the <code>++</code> operator",
  "testString":
    "assert(/[+]{2}\\s*myVar|myVar\\s*[+]{2}/.test(code), 'Use the <code>++</code> operator');"
},

But then another test:

{
  "text": "<code>myVar = myVar + 1;</code> should be changed",
  "testString":
    "assert(/var\\s*myVar\\s*=\\s*87;\\s*\\/*.*\\s*myVar\\+\\+;/.test(code), '<code>myVar = myVar + 1;</code> should be changed');"
},

fails for the pre-increment. [rolls eyes]