JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

Nayoon kim
Nayoon kim
3,098 Points

I Would like to know why I have to repeat defining the 'questionsLeft' here to work // questions -= 1; //

// Here are my codes and I have TWO questions

var questions = 3; var questionsLeft = ' [There are '+ questions + ' questions left ]'; var adjective = prompt('Please type an adjective'+ questionsLeft);

questions -= 1 ; questionsLeft =' [There are '+ questions + ' questions left ]';
//** I feel like there is no need for me to define here as it is already done up there. but it //didn't work without this sentence and I wonder why. var verb = prompt('Please type a verb'+ questionsLeft );

questions -= 1; questionsLeft =' [There are '+ questions + ' questions left ]'; var noun = prompt('Please type a noun'+ questionsLeft);

if (questions=1) { alert('All done. Ready for the message?'); // * *Also I have put --- break; ---- here and it didn't work and I don't know why I cant put it here *// }

var sentence = "<h2>There once was a " + adjective; sentence += ' programmer who wanted to use JavaScript to ' + verb; sentence += ' the ' + noun + '.</h2>'; document.write(sentence);

1 Answer

Steven Parker
Steven Parker
177,727 Points

It's necessary to reassign questionsLeft whenever questions changes for it to reflect the current value.

But you could eliminate that variable entirely by extending the prompt argument like this:

var verb = prompt('Please type a verb [There are ' + questions + ' questions left]');
Nayoon kim
Nayoon kim
3,098 Points

ahhh you helped me with all the questions that I had today ! thanks a lot and your willpower and persistence to learn how to code inspired me as well ! thank you