JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

Benis T
PRO
Benis T
Pro Student 3,595 Points

Count down not working

What is wrong with this snippet?

var questionCount = 3;
var questionsLeft = '[' + questionCount + ' questions left]';
var adjective = prompt('Please type an adjective ' + questionsLeft);
questionCount -= 1;
var verb = prompt('Please type a verb ' + questionsLeft);
questionCount -= 1;
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);

MOD: Edited question to format the code.

2 Answers

Steven Parker
Steven Parker
177,602 Points

It's a bit hard to read without formatting (there's a video on code formatting), but I can see that "questionsLeft" is not being updated between questions, so each prompt will say "[3 questions left]".

A quick fix would be to reset it each time "questionCount" is changed:

questionCount -= 1;
questionsLeft = "[" + questionCount + " questions left]";
Victor Ibasco
Victor Ibasco
11,035 Points

Hey friend ! Small fixes here. It would be much easier if I knew how to insert formatted text, but here I go. I added comments to describe issues. I believe the key issue here was your train of thought when using the variable 'questionsLeft'. You cannot iterate a string and that is what 'questionsLeft' is--a string. You can iterate a number ! If you want clarification go ahead and reply and hopefully I'll get a notification.

Cheers !

var questionCount = 3; 
var questionsLeft = '[' + questionCount + ' questions left]'; 
var adjective = prompt('Please type an adjective ' + questionsLeft); // use questionCount instead and add + 'questions left'
questionCount -= 1; 
var verb = prompt('Please type a verb ' + questionsLeft); // questionCount and add + 'questions left'
questionCount -= 1; 
var noun = prompt('Please type a noun ' + questionsLeft); // questionCount and add + 'questions left'
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);

The code after fixed (multiple solutions): My solution was that I made questionsLeft a string and added questionCount. One thing I didn't do was keep the brackets []. You could add those in.

var questionCount = 3; 
var questionsLeft = ' questions left'; 
var adjective = prompt('Please type an adjective ' + questionCount + questionsLeft);
questionCount -= 1; 
var verb = prompt('Please type a verb ' + questionCount + questionsLeft);
questionCount -= 1; 
var noun = prompt('Please type a noun ' + questionCount + 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);

MOD: Edited answer to format the code.

Steven Parker
Steven Parker
177,602 Points

See the video I recommended to Benis for instructions on code formatting.