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

Madlibs challenge

Hi guys, in the madlibs challenge in the JS course there is a line of code that confuses me a bit . Guil Hernandez wanted to add a counter that showed the number of questions left. So he created a new variable named questionsLeft and every time he wanted to subtract a question from questionsLeft he wrote questions -= 1 on the line after the prompt variable that prompted the user to enter a word then directly below that he updated the questionsLeft variable. However, he didn't type the var keyword, he just typed questionsLeft and that didn't create any problem with the code.

But in his example he only asked for one adjective, one verb, one noun. I wanted to ask for several adjectives and verbs and so on and I was trying to use the same method he used but I'm getting an error where if I use the same word, i.e. adjective it adds the new word from the prompt to the first adjective the user provided. Does that mean for each adjective I want I have to create a new variable with a new name, even if they are all adjectives? isn't there a way to ask for many adjectives and simply update the adjective with the new word? because if I use my method where in the sentence variable I just type adjective it concatenates all the adjectives provided by the user and puts that long nonsensical word into every area of the sentence that has the variable adjective. And furthermore, the last adjective the user typed is placed in the first slot of the sentence and the last and the first adjective they typed sort of gets replaced by the last. Is there a way to update the adjective variable with the new adjective and place it in the correct place in the sentence? I don't know if you understand my question so I'll add the code below. Thanks.

var questions = 4;
var questionsLeft = ' [' + questions + 'questions Left]';
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';
var verb = prompt('Please type a verb.' + questionsLeft);
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';
var noun = prompt('Please type a noun.' + questionsLeft);
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';
adjective = prompt('Please type another adjective.' + 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 + '.';
sentence += ' The programmer had a ' + adjective + '</h2>';
document.write(sentence);

1 Answer

Steven Parker
Steven Parker
243,318 Points

If you want to keep the current structure of asking all questions before building the sentence, you will need a new variable for each word (no matter what type of word). So in this case you might have "adjective1" and "adjective2".

The alternative would be to build the sentence in parts as each question is asked, in which case it would be possible to re-use the same variables. But this might make the program a bit harder to read and maintain.

Thank you Steven Parker. Your answer makes sense to me and I'm going to try it out. I will share the code once I do and if it works.