JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

Some clear code with comments to help you understand this video!

Hey guys, I've struggled for hours to understand this video because like many of you I also feel like there is a big jump in difficulty compared the video before this one.

I think there are a couple of reasons why this video was hard to understand for me (and probably you too), the code was very hard to read and thus hard to understand. So let this hardship also be a very wise lesson for all us students is to always strive for readable code.

Here is the code that I eventually came up with a lot less short-hand annotations for better readability, added to that I've also placed comments everywhere to help you understand the code even more.

Hope this helps!

//Start Javascript
console.log("Javascript initialised");



//Question left 3
var questions = 3;
var questionsLeft = '[' + questions +' questions left]';

var askAdjective = prompt('Please type an adjective' + questionsLeft);


//Question left 2
var questions = questions -1;
questionsLeft = '[' + questions +' questions left ]';

var askVerb = prompt('Please type a verb' + questionsLeft);


//Questions left 1 
var questions = questions -1;
questionsLeft = '[' + questions + ' questions left]';

var askNoun = prompt('Please type a noun' + questionsLeft);

//Alert message
alert('All done. Ready for the message?');

//Sentence assembly
var sentence = "<h2>There once was a " + askAdjective;
sentence += ' programmer who wanted to use JavaScript to ' + askVerb;
sentence += ' the ' + askNoun + '.</h2>';

//Prints the code into your browser view
document.write(sentence);



//Finish Javascript
console.log("Javascript stopped");

Enjoy the road folks, don't let the anxiety get the better of you!

Antti Lylander
Antti Lylander
9,660 Points

your code works, but you should not declare the same variable many times.

var questions = questions -1; -> questions += -1 or even questions -= 1

Just like you did with 'sentence'

Kimberly Dolcin
Kimberly Dolcin
4,369 Points

why did you not include closing and opeing h2 tags in your code?

I was thinking in the same way, that it should be something with "-" .Thank you it works!

Doron Geyer
Doron Geyer
Full Stack JavaScript Techdegree Student 7,415 Points

I think the confusion comes in with 2 things at least for me as a brand new programmer.
1) I had no idea what the <h2> </h2> tags were for as it isnt covered in the techdegree for FSJS yet ( unless I missed it) with a bit of googling realised its for heading level 2? Until now I thought that it was HTML and therefore those kind of things go into the HTML file. Guess not. 2) The use of [ brackets in quote marks after having seen someone that they are used for arrays made it a confusing thing for me. I couldn't figure out why hes using square brackets until I realized hes just using it as a string.

your break down with the coloring highlighted the [ being a string not an actual [. thanks for that

Greg Gancarz
Greg Gancarz
3,486 Points

Much easier to comprehend seeing it in this formatting. Helpful post!

1 Answer

Why for some reasons I can't get my browser to pop up the alert box like it shown in the video? I copied everything out of the video, but had not success.

'''html <p>https://teamtreehouse.com/workspaces/38312602#</p> '''