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 trialAnnalee Pawlowski
1,144 Pointsresponse = prompt(question); is not storing users input. Instead it stores the but question string from the array.
I have followed this tutorial code exactly and keep running into the same error. The response = prompt(question); is storing the original question string from the quizQuestions array rather than what the users typed in answer is, therefore :
if (response === answer)
will always return false.
The Also had this problem when trying to code prior to watching the answer video... What am I doing wrong?
// My Code
var quizQuestions = [ [ 'What is the color of the sun?' , 'Yellow' ] , [ 'What is the color of the grass?' , 'Green' ] , [ 'What is the color of the sky?' , 'Blue'] , ]
var correctAnswers = 0; var question; var answer; var response; var html;
function print(message) { document.write(message); }
// Ask the questions
for (var i = 0; i < quizQuestions.length; i += 1) { question = quizQuestions[i][0]; answer = quizQuestions[i][1]; response = prompt(question); response = question.toLowerCase();
if (response === answer) { correctAnswers += 1; }
}
html = "You got " + correctAnswers + " question(s) correct."; print(html);
// You can see it is incorrect by console.log response or by getting the answers right and noting that they are not added to the correctAnswer count.
1 Answer
Steven Parker
231,269 PointsThe prompt isn't the issue, but the line after it is replacing the result with the question:
response = prompt(question); // this gets the user's answer into "response"
response = question.toLowerCase(); // this replaces "response" with question (in lower case)
Perhaps you intended to convert the reponse to lower case. You can do that on the same line:
response = prompt(question).toLowerCase();
Also, to format posted code as shown here, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.