Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Working With Numbers The Mad Libs Challenge Revisited

Number of questions not subtracting

The code below runs, but the number of questions after each prompt never goes down, i.e. it always reads "[3 questions left]". I thought I was identical to the example in the video, but would appreciate a set of fresh eyes. Thanks!

Code:

var questions = 3; 
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);
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);

3 Answers

adamdonatello
adamdonatello
27,485 Points

Hi Stephan,

It seems that you had a couple of typos in your code. Some of your "questionsLeft" variables didn't match up. I have modified your code for you below.

var questions = 3; 
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); 

alert('All done. Ready for the message?'); 
var sentence = "There once was a " + adjective; sentence += ' programmer who wanted to use JavaScript to ' + verb; sentence += ' the ' + noun + '.'; 
document.write(sentence);

Hope this solves your issue!

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

Each time your prompt references the variable questionsLeft which remains the same throughout the program. It should reference questions as that is the one you are subtracting throughout.

Cheers

Rogelio Valdez
Rogelio Valdez
6,244 Points

Check for typos.

For example: These are two different variables only because of one Capital or Lowercase letter.

var myVariable = 10;
var myvariable = 90;