Basic JavaScript: Increment a Number with JavaScript

var myVar = 87;

// Only change code below this line

var myVar = myVar ++;

var myVar = 88;

but i keep getting this error message :

/ / running tests

myVar = myVar + 1; should be changed

// tests completed

please help…

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

  • You have a space between myVar and ++.;
  • You are redeclaring your variable 3 times.
  • myVar++ does not require assignment (=)
  • You are hardcoding a value to myVar at the end, which makes all the preceding code pointless.
2 Likes

it shows the same error when i removed the space

The expression myVar++ by itself increments myVar variable, like myVar = myVar + 1 does.
You don’t need myVar = before it.

3 Likes

wow, thanks i solved it

Technically, isn’t the myVar++ operation mostly equivalent to returning myVar and then incrementing it by 1? myVar = myVar + 1 is more equivalent to a pre-increment like ++myVar or myVar+=1 instead of post-incrementing the variable as in myVar++, correct?

I am not as familiar with JavaScript as a more OOP language like Java, so I might be wrong.

Yes, but that’s beyond what campers have learned so far at this point. As a stand-alone statement, i++; is functionally equivalent to i = i + 1.

Okay, I think a small disclaimer should be given to say it is only loosely equivalent perhaps. That is just my two cents though.

Thanks to everyone who contributes!

Technically, you’re right. But, as Ariel said, in this case it’s the same as myVar = myVar + 1. But good call, I edited my post to be more precise.

1 Like

It isn’t exactly an unimportant distinction, however.

Arithmetic operators

// Postfix 
var x = 3;
 y = x++; // y = 3, x = 4

// Prefix
var a = 2;
b = ++a; // a = 3, b = 3

Let’s say you have the wrong type, you got a string but you thought it was a number.

var shouldHaveBeenANumber = 'test'
shouldHaveBeenANumber++
returns NaN

var shouldHaveBeenANumber = 'test'
shouldHaveBeenANumber = shouldHaveBeenANumber + 1
returns "test1"

But I agree it is mostly irrelevant at this point in the curriculum (probably a pointless post to make, sorry).

1 Like

I agree that it can lead to confusion down the road if someone believes it is exactly the same in equivalence from the moment they learn about the operator. Hence why I believe they need to know it is only loosely the same when they learn about it.

The challenge may just need a note along the lines of “The increment operator actually has a bit more to it, which you’ll learn about in a later challenge, but for now what you need to remember is that it’s shorthand for adding a 1 to the variable.”

1 Like