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
SeHyun Choi
3,441 PointsI am trying to loop old project
I need help. I think I the question is looping correctly. I think the problem I am having is whatever I typed in third question (type a verb) is stored in the Variable answerr. What I want it to do is my response to every question to be stored.
CODE
var question = 3;
var questionsLeft = " [There are " + question + " question(s) left.] ";
var questionz = [
"Type a noun",
"Type an adjective",
"Type a verb"
];
var questionn;
var answerr;
function loopIt(arr) {
for(i = 0; i < arr.length; i += 1) {
questionn = prompt(arr[i] + questionsLeft);
answerr = questionn;
question = question -1;
questionsLeft = " [There are " + question + " questions left.] ";
}
}
loopIt(questionz);
function print(message) {
var div = document.getElementById("output");
div.innerHTML = message;
}
var html = "There once was a " + answerr + ".";
html += " The " + answerr + " was very " + answerr;
ahtml += " and liked to " + answerr + ".";
print(html);
1 Answer
Steven Parker
243,173 PointsThe loop asks 3 questions, and uses an array of 3 strings for the prompts. But there's only one variable to hold the answers, so each answer given replaces all those before it.
So to save all the answers, you might want to use another array to hold them. The code might look like this:
// before the loop
var answer = [];
// inside the loop
answer.push(questionn);
// after the loop
var html = `There once was a ${answer[0]}.
The ${answer[0]} was very ${answer[1]}
and liked to ${answer[2]}.`;
SeHyun Choi
3,441 PointsSeHyun Choi
3,441 PointsThank you.