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 trial

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

Alex Thomas
Alex Thomas
4,247 Points

quiz challenge 1

https://w.trhou.se/hnwrk0wy78

I used words and numbers in my answers, but at the end I still get 0 answers correct. Can anyone help out?

4 Answers

Rich Zimmerman
Rich Zimmerman
24,063 Points

It's because you're trying to use parseInt on the response, no matter the question. So if an answer is given that is a string, it will return NaN (Not a number).

It looks like in the video all the questions have answers that are integers, so parseInt works. Whereas you have questions that require strings as answers. So you can just remove the parseInt method and it should work.

Alex Thomas
Alex Thomas
4,247 Points

Hi and thanks Rich. I realized that after going over my code while watching the solution video.

Niki Molnar
Niki Molnar
25,698 Points

Remove parseInt() from the response in your for loop, so it should be

response = prompt(question);

Then it works fine

Alex Thomas
Alex Thomas
4,247 Points

HI and thanks. I did remove parseInt and it seems to work somewhat. How do I add multiple answers? for example in question one I did this:

["What is the name of Han Solo's ship?", "The Millenium Falcon" || "the millenium falcon"],

but if I type on the second answer it won't record it as correct. I wanted someone to be able to type in either all lower or upper case or a mix of the two, and also if they just typed "millenium falcon" i'd want that to be recorded as correct as well. I used || as I thought that would work but to no avail. But thank you for response.

Rich Zimmerman
Rich Zimmerman
24,063 Points

Your best bet is to convert it to lower case at the time of comparison and have the answer stored as lower case in the array.

question = ["What is the name of Han Solo's ship?", "the millenium falcon"];

if (response.toLowerCase() === answer) {
  // something
}
Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Alex,

I've forked the Workspace and modified it to do what you want, I believe. I left a few comments to explain the changes. Please feel free to contact me if you have questions.

You can checkout the updated Workspace here.

I hope this helps.

Happy Coding :)

Alex Thomas
Alex Thomas
4,247 Points

Hey, thanks a lot Justin. That's great.

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

Hi Alex, I can maybe help you by pointing that you're using the || operator in a way that doesn't fit your expectation. When you write this :

var questions = [
  ["What is the name of Han Solo's ship?", "The Millenium Falcon" || "the millenium falcon" || "Millenium Falcon" || "millenium falcon"],
  ["How fast did it make the Kessel Run?", "12" || "12 parsecs" || "12 Parsecs"],
  ["Was it fast enough for you old man?", "Yes" || "yes"]
 ];

you're using the OR operator to express the fact that it's either this answer or another one. But in JavaScript the OR operator is a logical operator and it doesn't care about semantics. What it does, in what you wrote, is evaluating the first string "The Millenium Falcon" as true or false (and not right or wrong!) and since it's not an empty string, it's evaluating it as true and then return "The Millenium Falcon" as the result of the whole expression "The Millenium Falcon" || "the millenium falcon" || "Millenium Falcon" || "millenium falcon". So what your really get is this array :

var questions = [
  ["What is the name of Han Solo's ship?", "The Millenium Falcon"],
  ["How fast did it make the Kessel Run?", "12"],
  ["Was it fast enough for you old man?", "Yes"]
 ];

See the exemples on MDN : ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR )

o1 = true  || true       // t || t returns true
o2 = false || true       // f || t returns true
o3 = true  || false      // t || f returns true
o4 = false || (3 == 4)   // f || f returns false
o5 = 'Cat' || 'Dog'      // t || t returns "Cat"
o6 = false || 'Cat'      // f || t returns "Cat"
o7 = 'Cat' || false      // t || f returns "Cat"
o8 = ''    || false      // returns false
o9 = false || ''         // returns ""

I hope it helped.

Raphaël