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
karan kumar
43 Pointswhy updating the value doesn't update globally?
<p>
var ques = 3;
var quesleft = '[ '+ques+' ques left ]';
var name = prompt("Name"+quesleft);// output Name 3 <br>
ques = ques-1;
var age = prompt("Age"+quesleft);//output Name 3
what is the reason when i have decreased the counter its not update the value ?
it should show Name 2 because after 1 statement value of ques decreases
</p>
1 Answer
Gonras Karols
17,357 PointsBecause quesLeft was defined when ques was equal to 3 and you haven't updated it after you updated ques. Variables don't work like this - they dont 'track' values of the variables that were assigned to them on initialization. You should update quesLeft right after a new value is assigned to ques, like this -
var ques = 3;
var quesleft = '[ '+ques+' ques left ]';
var name = prompt("Name"+quesleft);// output Name 3
ques = ques-1;
quesleft = '[ '+ques+' ques left ]';
var age = prompt("Age"+quesleft);//output Name 2