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 (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Danilo Rodriguez
Danilo Rodriguez
1,663 Points

Variable in variable in alert not working?

I haven't seen the answer video yet I'm trying to figure out why this doesn't work. If I write out the variable correctMessage (as in Question 2) then correct += 1 works properly and the intended number is displayed. If I try to call the variable correctMessage (as in question 3) then correct +=1 is ignored and only 0 is displayed.

var correct = 0 var correctMessage = "Right! " + correct + " of 5 correct."

// Question 2

var motoAnswer2 = prompt("What motorcycle?") if (motoAnswer2==='gsxr600') {

correct += 1; alert("Right! " + correct + " of 5 correct."); } else {alert(wrongMessage)}

// Question 3

var motoAnswer3 = prompt("color motorcycle?") if (motoAnswer3==='red'){

correct += 1; alert(correctMessage); } else {alert(wrongMessage)}

also, for future reference, how do I display my code when asking for help without copy and pasting here?

2 Answers

Danilo Rodriguez
Danilo Rodriguez
1,663 Points

I found my answer in the next next video lol. What I wanted is something that wasn't taught yet. instead of having correctMessage as a variable with a variable in it, it needed to be a function.

Steven Parker
Steven Parker
229,644 Points

The "correctMessage" string was created when the program first started, and it used the value of "correct" at that time (which was 0). After that, changing "correct" will not change the string in "correctMessage".

The only solution is to rebuild the string as you did for question 2 to get the current value of "correct" to show.