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 trialRandy Singh
Full Stack JavaScript Techdegree Student 1,462 PointsReview of Quiz: I run into an error where the console says "cannot read property of 'toLowerCase' of undefined.
function print(message) {
document.write(message);
}
var questionCorrect = 0;
var questionWrong = 0;
var question;
var answer;
alert('Welcome to the Two Dimensional Array Challenge Quiz');
var questions = [
['What is the best language to learn?', 'javascript' ],
['What do you want to be?', 'front end web developer' ],
['Where do you work?', 'vitals' ]
];
for ( var i = 0; i <= questions.length; i += 1 ) {
question = prompt(questions[i][0]);
if (answer.toLowerCase() === questions[i][1]) {
alert('correct answer. move on to the next question');
questionsCorrect +=1;
}
else {
alert('wrong answer. move on to the next question');
questionWrong +=1;
}
}
print('you got ' + questionCorrect + ' questions right');
print('you got ' + questionWrong + ' questions wrong');
Im pretty sure this code would run smoothly. Im just not sure why it gets stuck at the toLowerCase() method...?
4 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Randy,
Your answer variable is empty... and you are not assigning anything to it to be checked by the toLowerCase()
method, which is why you are getting the "undefined" error.
I would recommend reviewing the video again to see how the value is assigned to the answer
variable with the prompt()
method.
Keep Coding! :)
john larson
16,594 PointsI suspect it's referring to this:
- if (answer.toLowerCase() === questions[i][1])
It could be there, but I don't see WHERE it is defined
- question = prompt(questions[i][0]);
is it possibly supposed to be:
- answer = prompt(questions[i][0]); ?
Randy Singh
Full Stack JavaScript Techdegree Student 1,462 PointsOh ok, so i wasn't actually assigning anything to my answer variable before I used it. However, now I am able to go through all my questions and submit answers, but after my final question/answer I get this: 'Cannot read property '0' of undefined' which points to this line in my code:
question = questions[i][0];
For reference this is my code now:
function print(message) {
document.write(message);
}
function print(message) {
document.write(message);
}
//variables
var questionCorrect = 0;
var questionWrong = 0;
var question;
var answer;
var response;
var arrayRight = [];
var arrayWrong = [];
//alert welcoming player to the quiz
alert('Welcome to the Two Dimensional Array Challenge Quiz');
//variable housing 3 arrays of questions and answers
var questions = [
['What is the best language to learn?', 'javascript' ],
['What do you want to be?', 'front end web developer' ],
['Where do you work?', 'vitals' ]
];
//For loop for the questions and answers array above
//It will iterate through each array
for ( var i = 0; i <= questions.length; i += 1 ) {
//asks the question in each array
question = questions[i][0];
answer = questions[i][1];
response = prompt(question)
/*
if the users answer matches the answer in the array, add 1 to the questionCorrect variable and
add this question to the arrayRight variable
*/
if (answer.toLowerCase() === questions[i][1]) {
alert('correct answer. move on to the next question');
questionCorrect +=1;
arrayRight += '<li>' + questions[i][0] + '</li>';
//arrayRight.push(questions[i][0]);
}
else {
/*
if the user gets the answer wrong, add 1 to the questionWrong variable and add this question
to the arrayWrong variable
*/
alert('wrong answer. move on to the next question');
questionWrong +=1;
arrayWrong += '<li>' + questions[i][1] + '</li>';
arrayWrong.push(questions[i][1]);
}
}
//prints out number of questions user got correct
print('you got ' + questionCorrect + ' questions right');
//prints out number of questions user got wrong
print('you got ' + questionWrong + ' questions wrong');
Im not understanding why it would ask me the 3 questions like I intended it to, but then error out where I actually defined what the variable question would be.....
john larson
16,594 PointsI see a few things wrong but I have to sleep...work in the morning. If someone else doesn't help you get it, I'll take another look tomorrow.
john larson
16,594 PointsI made some changes. It seems to work now. One part I don't get why it helped was I got rid of the <= in the for loop and just made it a <. Other changes I can explain if you like.
function print(message) {
document.write(message);
}
var questions = [
['What is the best language to learn?', 'javascript' ],
['What do you want to be?', 'front end web developer' ],
['Where do you work?', 'vitals' ]
];
//variables
var questionCorrect = 0;
var questionWrong = 0;
var question;
var answer;
var response;
var arrayRight = [];
var arrayWrong = [];
alert('Welcome to the Two Dimensional Array Challenge Quiz');
for ( var i = 0; i < questions.length; i += 1 ) {/*got rid of =*/
question = questions[i][0];
answer = questions[i][1];
response = prompt(question)
if (response.toLowerCase() === answer) {/*changed this line*/
alert('correct answer. move on to the next question');
questionCorrect +=1;
arrayRight.push(question)/*added this*/
// arrayRight += '<li>' + questions[i][0] + '</li>';remove this
//arrayRight.push(questions[i][0]);removed this
}
else {
alert('wrong answer. move on to the next question');
questionWrong +=1;
arrayWrong.push(question)/*added this*/
// arrayWrong += '<li>' + questions[i][1] + '</li>';remove this
// arrayWrong.push(questions[i][1]);
}
}
print('you got ' + questionCorrect + ' questions right');
print('you got ' + questionWrong + ' questions wrong');