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 trialBenis T
Courses Plus Student 3,595 PointsCount 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
231,271 PointsIt'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
11,035 PointsHey 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
231,271 PointsSee the video I recommended to Benis for instructions on code formatting.