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 trialChris Harkin
1,104 PointsBuild a Quiz Challenge, Part 1 code wont work
Hay here is my solution. It was working when I tested the prompt boxes to get the users answers. But no longer works at all can anyone help.
// Two Dimensional array soring questions and answers.
var questionAnswers = [
["What color is the sky", "Blue"],
["What color is Grass", "Green"],
["Waht color is the Sun", "Yellow"]
];
// variable users answer, correct answer and wrong answers
var userAnswer = [];
var correctAnswer = [];
var wrongAnswer = [];
// check the users answers
for (i = 0; i , questionAnswers.length i += 1) {
userAnswer = prompt (questionAnswers [i][0]);
if (answer === questionAnswers [i][1]){
correctAnswer.push (questionAnswers [i][0]);
}else{
wrongAnswer.push (questionAnswers [i][0]);
}
}
// tell the user how many questions the got correct and which questions where correct and which question they got wrong.
document.write ("<h1>Congratulations you got " + correctAnswer.length + " correct</h1>");
document.write ("<h2>Congratulations you answerd these questions correctly: </h2>" + correctAnswer.join ('<br>'));
document.write ("<h2>Sorry but you got these questions wrong: </h2>" + wrongAnswer.join ('<br>'));
function print(message) {
document.write(message);
}
2 Answers
Snehalnayan Vaishnav
798 Points// Two Dimensional array soring questions and answers. var questionAnswers = [ ["What color is the sky", "Blue"], ["What color is Grass", "Green"], ["Waht color is the Sun", "Yellow"] ];
// variable users answer, correct answer and wrong answers var userAnswer = []; var correctAnswer = []; var wrongAnswer = [];
// check the users answers for (i = 0; i < questionAnswers.length; i += 1) { userAnswer = prompt (questionAnswers[i][0]); if (userAnswer === questionAnswers[i][1]){ correctAnswer.push (questionAnswers[i][0]); }else{ wrongAnswer.push (questionAnswers[i][0]); } }
// tell the user how many questions the got correct and which questions where correct and which question they got wrong. document.write ("<h1>Congratulations you got " + correctAnswer.length + " correct</h1>"); document.write ("<h2>Congratulations you answerd these questions correctly: </h2>" + correctAnswer.join ('<br>')); document.write ("<h2>Sorry but you got these questions wrong: </h2>" + wrongAnswer.join ('<br>'));
function print(message) { document.write(message); }
Kelsea Anderson
12,632 PointsThere are a couple of errors in the for loop. It needs var, a less than operator, and a semicolon. It should look something like this
for (var i = 0; i < questionsAnswers.length; i += 1) {...}
Hope that fixes it!