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 trial

JavaScript JavaScript Basics Storing and Tracking Information with Variables Change the Value in a Variable

Forrest Pollard
Forrest Pollard
1,988 Points

Cannot seem to correctly change the value of a variable from a prompt

Was experimenting with what i learned from this lesson by trying to do something myself but I am struggling. here is my code, i think you will understand what I have been trying to do,

var score = 0;
score += 10, score +=5;
var bonusPoints = 0;
bonusPoints += prompt("input desired bonus points");
score += bonusPoints;
alert(score);

however it keeps adding whatever is input in the prompt as a string at the end of the score value. Any help is greatly appreciated, many thanks :)

1 Answer

Austin Whipple
Austin Whipple
29,725 Points

Hi Forrest,

By default, the value submitted to the prompt you're displaying is going to be stored as a string. Using += is going to produce strange results using anything that's not an integer. Luckily, JavaScript has some handy functions to convert strings to integers/numbers! I suspect you can use parseint() without much problem, but perhaps Number() would work as well. Here's a StackOverflow article with lots more information.