Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Linda Fennigbauer
Linda Fennigbauer
5,681 Points

Counter not working and .toLowerCase question

Hi,

When the questions are finished and the program prints to screen the counter always says "You got 5 questions right" even if I made sure not to get any right. What have I done wrong? Also I want to make sure that even if the user write the answer with a capital at the start that it will still log as an correct answer so I've added .toLowerCase in my code, have I done this correctly? Thanks!

// var's 
var correctAnswers = 0;
var question;
var answer;
var response;
var html;


// question and answers array
var questions = [
['What is the strongest muscle in the human body? The tongue or bicep?', 'tongue'],
['Who invented the electric chair? A carpenter or dentist?', 'dentist'],
['What is the line called between 2 numbers in a fraction? Vinculum or Fractoral?', 'vinculum'],
['Which body part stays the same size from when we are born until we die? Heart or Eyeball?', 'eyeball'],
['What was the first novel written on a typewriter?, To Kill A Mockingbird or Tom Sawyer?', 'tom sawyer']
];

for (var i = 0; i < questions.length; i += 1) {
    question = questions[i][0]; 
    answer = questions[i][1]
    response = prompt(question);
    response = answer;
    if (response === answer.toLowerCase()) {
        correctAnswers += 1;
    }
}

html = "You got " + correctAnswers + " question(s) right.";
print(html);





function print(message) {
  document.write(message);
}```
Linda Fennigbauer
Linda Fennigbauer
5,681 Points

I managed to get the counter working, yay! I realised I had a bit of code that wasn't needed (response = answer) but I still can't get the answers to log as correct if they are typed with capitals. So help with this would be great. Thanks!

1 Answer

William Tan
William Tan
5,148 Points
var correctAnswers = 0;
var question;
var answer;
var response;

// question and answers array
var questions = [
['What is the strongest muscle in the human body? The tongue or bicep?', 'tongue'],
['Who invented the electric chair? A carpenter or dentist?', 'dentist'],
['What is the line called between 2 numbers in a fraction? Vinculum or Fractoral?', 'vinculum'],
['Which body part stays the same size from when we are born until we die? Heart or Eyeball?', 'eyeball'],
['What was the first novel written on a typewriter?, To Kill A Mockingbird or Tom Sawyer?', 'tom sawyer']
];

function print(message) {
  for (var i = 0; i < questions.length; i += 1) {
    question = questions[i][0];
    response = prompt(question);
    answer = questions[i][1]
    // response = answer;
    if (response.toLowerCase() === answer) {
        correctAnswers += 1;
    }
}
message = "You got " + correctAnswers + " question(s) right.";
document.write(message);
}
print();

The .toLowercase() method should have been called on the variable response. (The response variable is what needed to be converted to lower case in order for it to be accurately compared to your answers. Which are already assigned in lowercase )

Linda Fennigbauer
Linda Fennigbauer
5,681 Points

Doh! So simple once it's pointed out and makes perfect sense. Thank you William.