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

some array items not being read, only show one question right when its run

I followed along because i wanted to try out dave's solution but.....i'm sure there is an error some where in my syntax but...i can not find it. When the code runs and you put in the prompts it doesn't read two of the array items, and when you are finished putting in the answers it comes up you only got one question correct.

var questions=[
  ['What is two plus two', 4],
  ['what is color is the sky', 'blue'],
  ['what color is made from blue and red', 'purple']
];

var correctAnswers=0;
var question;
var answer;
var response;
var html;

function print(message) {
  document.write(message);
}

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


html='you got ' + correctAnswers+ ' question(s) right.';
print(html);

2 Answers

Two minor issues:

1) The first is that you are converting the users responses to integers which won't work for "blue" and "purple" so you get those wrong. That's why you only get 1 correct.

// wrong: converting every answer to a number
response = parseInt( prompt(question) );
if( response === answer ){ ... }

//correct
response = prompt(question);
if( response === answer ){ ... }

2) Now you would have to convert 4 to a string because "4" === 4 is false. You would only ever get 2 correct answers. OR you could change to double equals to compare the response to the answer ( "4" == 4 will be true).

// nope
['What is two plus two', 4]

// OK
['What is two plus two',  '4']

well awesome i'm learning things i didn't catch before, you have cleared up a few questions i had about parseInt in a few minutes, thank you!