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

4 Answers

alexlitel
alexlitel
25,059 Points

Mantas Gudauskas The parseInt(prompt(question)) method in the for loop is intended to convert the string answer from the prompt into a number. In the video, all the answers are numbers, so the method is appropriate. All your answers, however, are strings, and aside from question two, none of these can be converted into numbers.

So you will need to slightly simplify your code and change the value of the response variable to the string the user types in the prompt. If you go to the response variable in your for loop and remove parseInt( method and the second closing parentheses after question, your code will work. Your loop should look like this:

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

If you wanted to use both numbers and strings for answers and use the parseInt method, you could add an OR operator and change your if statement to:

if ( response.toLowerCase() === answer || parseInt(response) === answer )
Jason Desiderio
Jason Desiderio
21,811 Points

Mantas Gudauskas - the problem looks like it's with your response variable.

Two things:

  1. Once you run the prompt through parseInt(), it turns into an integer. Since there are no letters, integers don't have a toLowerCase() method - only strings have that method. This is throwing the error which can be seen inside the developer console.
  2. Not all of your answers in the questions array are integers so it wouldn't be appropriate to use parseInt() on every response.

If you change it to the code below, your program will function as desired

response = prompt(question);
Jeremy Fisk
Jeremy Fisk
7,768 Points

I think it should work if you remove the parseInt from line 20 of you code, and just make it state response = prompt(question);