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 1 Solution

Several Questions

Greetings! I'm having a few issues with this exercise. I wanted to try adding a few other fundamentals, such as printing the questions, answers, and responses to the page. I commented my issues in the code. Thanks for taking a look!

var questions= [
  ['What is 2+2?','4'],
  ['What is 4+2?','6'],
  ['What is the wind/speed velocity of a swollow?',
   'an african swollow or english swollow?']
]
var rightAnswer=0;
var wrongAnswer=0;
var response;
var html;
//I copied 'html' and 'response' variables from the video. I originally only had 2 variables, with an 'answer' variable inside the For Loop. Why didn't that work?
function print(message) {
  document.write(message);
}

for (i=0; i < questions.length; i+=1){
   response=(prompt(questions[i][0])).toLowerCase(); 
  print ((questions[i]) + '<br/>' +'. Your answer was '+ response+ '.<br/>');  
// ^--------------Line 19- 'questions[i]' prints both the question AND answer from 'questions'. Why is this?  

  //  response=response.toLowerCase();     <---------------?
//  I stole the method used on line 20 from the comments. Why would lie 25 not work?
}
  if (response == (questions[0][1] || questions[1][1]) || questions[2][1]){
    rightAnswer+=1;
    console.log (rightAnswer);
  }else{
    wrongAnswer+=1;
    console.log(wrongAnswer);
  }


html=("Wow! You got ' + rightAnswer + ' correct, and ' + wrongAnswer + '  wrong. Good job!")
print (html)
// ^-- This doesn't print 'rightAnswer' or 'wrongAnswer'. I've tried parseInt various places, changing to 'document.write', and added/ subtracted my conditional statment into the For loop. I have never gotten it to print anywhere correctly. Even the piping and double quotes in line 25 are part of this attempted fix (when my conditional statment was in the For Loop, line 25 would have read: 
//  if (response === (questions[i][1]){ 

2 Answers

Tom Sager
Tom Sager
18,987 Points

Line 19:

questions is a two-dimensional array.

questions[i] is a one-dimensional array with two elements. This is what you are printing - both elements.

questions[i][0] is the question, and questions[i][1] is the associated answer.

Line 24:

You are OR'ing the answers together, then seeing if the result is equal to response.

You want:

if (
  (response == questions[0][1]) ||
  (response == questions[1][1]) ||
  (response == questions[2][1])
)  {
   // correct
} else {
   // incorrect
}

Line 33:

The parentheses are not properly matched in the html = "..." statement. Everything between the two double quotes is a single string. i.e. the whole thing is one long string. Try using either single or double quotes to separate the strings from the variables, e.g.

html = "Wow! You got " + rightAnswer + " ... "

or

html = 'Wow! You got ' + rightAnswer + ' ... '

Depending on the editor you are using, the colors of the individual words should indicate when quotes are matched up properly.

I hope this helps!

Thank you Tom! Everything you said worked. I'm kind of glad it was minor fixes, and that I was closer than I thought. I was using Workspaces at the time, but when i put it into Sublime some of the differences showed up.

Tom Sager
Tom Sager
18,987 Points

Can you please vote this as 'best answer'. Thanks!