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

Madlibs and For Loop

I know that the instructor indicated that we may cover loops in a later stage of the course, but I was wondering how one might approach this coding exercise using the for loop.

I was thinking about this too! I think you might need an array and a for loop. Actually, I just did some hacking and needed two arrays.

var index;
var text;
var answers = []
var words = ["noun", "adjective", "verb",];
for (index = 0; index < words.length; index++) {
    text = words[index];
    answers[index] = prompt('Please provide a' + text+'?');
    console.log(text);
    console.log(answers[index]);}
document.write( answers[0] + answers[1] + answers[2] );

You could always add all of the sentence frills to polish it up, but you end up with an array filled with the users answer. I don't know if this is the most elegant way to do this, but I've only done a little of the JavaScript course so it's what I could manage.