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

Working on updating JavaScript challenges.

I just finished the Loops, Arrays, and Objects course and decided I would go back through the old challenges I saved and update them with my new knowledge of JavaScript. However when working on the Mad Libs Challenge I cant seem to access the answer in my object to print out correctly it always prints the last answer received. Ie: If the adjective were young, the verb were change and the noun world. It would print "There once was a world programmer who wanted to use JavaScript to world the world.". My mind is completely blank on what to do to get it to access the individual answers. Any insight is deeply appreciated.

Josh

console.log("Script Begins");

var message = " ";
var answer;
var input;
var questionTotal= 3;
var questionsAsked=[
    {question: "Allright great! First I'll need an adjective.",
     userAnswer: " 1"
    },
    {question: "Great now next I will need a verb.",
    userAnswer: " "
    },
    {question: "Perfect, now for the last step I will need a noun.",
    userAnswer: " "
    }
];

function print(message){
  var div = document.getElementById("output")
  div.innerHTML = message;
}

function fullStory(answer){
var story= "<h2> There once was a " + answer.userAnswer + " programmer who wanted to use JavaScript to " + answer.userAnswer + " the " + answer.userAnswer + ".</h2>";
return story 
}

while(true){
  input = prompt("Are you ready to help me tell a story? Type yes or no.");
  if(input.toLowerCase() === "no" || input === null){
    alert("Ok, thanks any way!");
    break;
  }else if(input.toLowerCase()=== "yes"){
    for (var i=0; i<questionsAsked.length; i+=1){
      answer= questionsAsked[i];
      answer.userAnswer[i]= prompt(answer.question+ " There are " + questionTotal + " question\(s\) left.");
      questionTotal-=1;
      message= fullStory(answer);
    }
    break;
  }
}
print(message);

1 Answer

Try this instead. If it works figure it out :)

console.log("Script Begins");

var message =[];
var answer;
var input;
var questionTotal= 3;
var questionsAsked=[
    {question: "Allright great! First I'll need an adjective.",
     userAnswer: " 1"
    },
    {question: "Great now next I will need a verb.",
    userAnswer: " "
    },
    {question: "Perfect, now for the last step I will need a noun.",
    userAnswer: " "
    }
];

function print(message){
  var div = document.getElementById("output")
  div.innerHTML = message;
}

function fullStory(cool){
var story= "<h2> There once was a " + cool[0]+ " programmer who wanted to use JavaScript to " + cool[1]+ " the " + cool[2]+ ".</h2>";
return story 
}

while(true){
  input = prompt("Are you ready to help me tell a story? Type yes or no.");
  if(input.toLowerCase() === "no" || input === null){
    alert("Ok, thanks any way!");
    break;
  }else if(input.toLowerCase()=== "yes"){
    for (var i=0; i<questionsAsked.length; i+=1){
      answer= questionsAsked[i];
      answer.userAnswer= prompt(answer.question+ " There are " + questionTotal + " question\(s\) left.");
      questionTotal-=1;
      message.push(answer.userAnswer);
    }
    break;
  }
}
var cool = fullStory(message)
print(cool)

// don't forget the semi colons :)

Jonathan Francisco thanks man! I didn't even think of storing the answers in another array worked great!