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) Working With Numbers The Mad Libs Challenge Revisited

Abdulrahman Mousa
Abdulrahman Mousa
2,276 Points

It is interesting that this doesn't work. Can you please validate if my theory is accurate?

From what I understand, this doesn't work because the questionsLeft variable has already accessed the questions variable in second line and stored the value in itself so it doesn't re-access the questions variable every time we the call the questionsLeft variable. Is there anyway to force a variable to revalidate the value(s) inside it ?

var questions = 3;
var questionsLeft = " [" + questions + " questions left]";
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1
var verb = prompt('Please type a verb' + questionsLeft);
questions -= 1
var noun = prompt('Please type a noun' + questionsLeft + questionsLeft);
alert('All done. Ready for the message?');
var sentence = "<h2>There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.</h2>';
document.write(sentence);

Thanks

Edited format of code

3 Answers

Matt F.
Matt F.
9,518 Points

Hello Khaled,

Yes, you are correct. Your string is assigned to questionsLeft on line two when questions is equal to 3. Even though questions is updated, the string will stay the same as when it was assigned.

One way to get around this is to write a reusable function that takes in the current value of questions and then builds the string when called.

See the getQuestionsLeft function below. It takes in a parameter, which it adds to the sentence template each time. We then call this function for each prompt whick passes in the questions variable with its current value.

var questions = 3;
function getQuestionsLeft (numQuestions) {
  return " [" + numQuestions + " questions left]";
};
var adjective = prompt('Please type an adjective' + getQuestionsLeft(questions));
questions -= 1
var verb = prompt('Please type a verb' + getQuestionsLeft(questions));
questions -= 1
var noun = prompt('Please type a noun' + getQuestionsLeft(questions));
alert('All done. Ready for the message?');
var sentence = "<h2>There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.</h2>';
document.write(sentence);
Abdulrahman Mousa
Abdulrahman Mousa
2,276 Points

Thanks.

I don't understand functions yet but I'll come back to this as soon as I get some exposure to functions.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It's been while since I looked at this course but I don't understand why this isn't working.

Variables are just that, Variable. Their values can be changed over time. If someone has a clever answer then I'd be interested in this too.

I'm also having the same issue, doesn't seem like the code works and I can't figure out why. I copied the code straight from the video and also tried the code above posted by Matt F. Really not sure what I'm doing wrong so any help would be great.

Matt F.
Matt F.
9,518 Points

Where are you trying the code that I posted? I just tested it again and the code executes as expected.

I used the workspaces in treehouse and just took a screenshot so you can see the code I've written. I copied the one above directly and just pasted it in. The other one I did along with the video so hopefully I got everything right. They both only ask the first two questions and the question countdown doesn't come up at all. (not sure how to add the screenshot sorry, I'm really new to this but will keep trying to add.)