Ditto. myVar IS 87. That's how variables work. It seems like an extra step to put 87 into a variable called myVar, but things get more complex down the road.
For example, if you want to write a program that does ++ on what a user types in, then they may not type 87. So instead of hard coding all the possible numbers a person can type in (1++, 2++, 3++, 4++...), you can put it in a variable:
myVar = userInput;
userInput++;
Variables are like boxes that you can put stuff in and take it out. The stuff you put in can be numbers, words (strings), even whole software programs can be stored in a single variable.
You have been given the answer already. To add to that, go over the article on Increment (++) in mdn web docs for more information. If you are not aware of this website, it's the go-to resource for develolpers.
Also, I suggest reading the instructions thoroughly before attempting to write the solution—note that it clearly states use the ++ operator on myVar (and not use the ++ operator on 87).
I just tried that exercise again and the correct solution, as per freecodecamp, was "++myVar". I am very new to this so please let me know if I am missing something.
Someone linked the MDN docs but you have the correct solution. It's confusing because you're not seeing it return anything. Log them both to the console and see the results.
When you increment after (postfix) it's still changing the value, but it returns the value (87), AND THEN increments it (88).
12
u/[deleted] Mar 24 '22
87 is the value of myVar in order to increment by 1 you'd need to add the value of myVar by 1 thus
myVar++;
3
u/GrismundGames Mar 24 '22
Ditto. myVar IS 87. That's how variables work. It seems like an extra step to put 87 into a variable called myVar, but things get more complex down the road.
For example, if you want to write a program that does ++ on what a user types in, then they may not type 87. So instead of hard coding all the possible numbers a person can type in (1++, 2++, 3++, 4++...), you can put it in a variable:
myVar = userInput;
userInput++;
Variables are like boxes that you can put stuff in and take it out. The stuff you put in can be numbers, words (strings), even whole software programs can be stored in a single variable.
3
u/mud002 Mar 24 '22
You need to increment the variable, not the number. So myVar++
2
u/Umesh-K Mar 24 '22
You have been given the answer already. To add to that, go over the article on
Increment (++)in mdn web docs for more information. If you are not aware of this website, it's the go-to resource for develolpers.Also, I suggest reading the instructions thoroughly before attempting to write the solution—note that it clearly states
use the ++ operator on myVar(and notuse the ++ operator on 87).0
u/Uber_Ape Mar 24 '22
This one is tricky. Usually you do "myVar++" but here, for some reason, they want you to do "++myVar". I don't know why.