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 trialSharad pareek
Courses Plus Student 681 Pointswhy it always say 0 correct answer
function print(message) { document.write(message); } var question_answer = [ ['what is your name?','sharad' ], ['what you want to doing future?','programmer' ], ['what you doing now?',' learn coding at treehouse'] ]; var correctAnswer = 0; var wrongAnswer = 0; var question; var answer; var html; for(i = 0;i<question_answer.length;i+=1) {
question = question_answer[i][0]; answer = question_answer[i][1]; answer = answer.toLowerCase; response = prompt(question); if(response === answer){ correctAnswer += 1; }else{ wrongAnswer += 1;
}
} html = 'you got ' + correctAnswer + ' correct answer.'; print(html);
1 Answer
Walter Allen
iOS Development with Swift Techdegree Student 16,023 PointsI think you need to use the equality operator (that is, ==) to compare the answer and response strings instead of the strict identity/strict equality operator (that is, ===). The strict equality operator is actually testing to see if they are the same object, which they are not. For the strict equality operator to show that two object are equal, they must both either be references to the same string OR both be primitive strings. (The array is storing a String object and not a primitive string.) You can learn more about this here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators, but that might be beyond your scope at this point. For now, try changing the === to == and see if that helps. If it doesn't, let me know and I'll see if I can look over it again. :)