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 trial

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

Jim McQuarrie
Jim McQuarrie
10,597 Points

Different way less code

As I am working on my JavaScript and trying different things, can anyone see a reason why this can not be written like I have it? Much less code, works the same!

var questions = 3;

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

1 Answer

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

You can totally write code like that but I don't think 3 lines of saving means much especially where underlying logic is same which is why it works same. However, I do think use of questionLeft in the video is tedious so I'll say yours is better although It seems like the code from video is just used as stepping stone for improvement on future video.

Jim McQuarrie
Jim McQuarrie
10,597 Points

This exercise was more about trying to do this in a different way and really get a much stronger grasp. I am not trying to out-do the Dave here as I know better :) , instead I am just trying to learn.