Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlessandro Sebastiani
3,021 Points+= Does not work in any of the workshop
Somebody could tell me why += operator for add a value in an exist variable does not work? var firstNumber = prompt("Give me the first number"); var firstNumber = parseInt(firstNumber); var secondNumber = prompt('Give me the second number'); var secondNumber = parseInt(secondNumber); var message = '<h1>Math with the number </h1>' + firstNumber + '<h1> and </h1>' + secondNumber; var message += firstNumber + secondNumber;
1 Answer
Ben Slivinn
10,156 Pointsvar firstNumber = prompt("Give me the first number");
var firstNumber = parseInt(firstNumber);
var secondNumber = prompt('Give me the second number');
var secondNumber = parseInt(secondNumber);
var message = '<h1>Math with the number </h1>' + firstNumber + '<h1> and </h1>' + secondNumber;
var message += firstNumber + secondNumber;
First thing first, please use the Markdown CheatSheet when placing code in treehouse, it's frankly unreadable otherwise.
Answer:
- You overwrite the 'message' variable.
- Example of using the addition assignment operator (+=):
var message = 1;
message += 5;
output: 6
With strings:
var message = "Hello ";
message += "Alessandro Sebastiani";
output: Hello Alessandro Sebastiani
Note, first i declare the variable, then i use the addition assignment operator on it.
Happy coding!
Alessandro Sebastiani
3,021 PointsAlessandro Sebastiani
3,021 PointsThank you very much ben ^^