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

christian bugay
christian bugay
4,260 Points

I'm not getting the the counter to add to the correctAnswers. Help please.

Code:

var questions = [ ["What's my name?", "CHRIS"], ["What's up?", "THE SKY"], ["What's good?", "NOT BAD"] ];

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

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

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

if (response.toUpperCase === answer) { correctAnswers += 1; } else { wrongAnswers += 1; } }

html = [["You got " + correctAnswers + " questions right."] + " " + ["You got " + wrongAnswers + " questions wrong."] ]; print(html);

2 Answers

Irving Amador
Irving Amador
7,488 Points

Oh yeah, youre missing 2 parenthesis after the toUpperCase function, like this:

var questions = [ ["What's my name?", "CHRIS"], ["What's up?", "THE SKY"], ["What's good?", "NOT BAD"] ];

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


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


for ( i = 0; i < questions.length; i +=1 ) { 
  question = questions[i][0]; 
  answer = questions[i][1]; 
  //alert(answer);
  response = prompt(question);
  //alert("response = " + response);

  if (response.toUpperCase() === answer) { 
    correctAnswers += 1; 
  } else { 
    wrongAnswers += 1; 
  } 
}


html = [["You got " + correctAnswers + " questions right."] + " " + ["You got " + wrongAnswers + " questions wrong."] ]; print(html);

I suggest using alerts to see what your code is doing when you are not getting the desire result.

Hope it helps.

Irving Amador
Irving Amador
7,488 Points

In this line:

          response = parseInt(prompt(question));

It seems you are trying to get an integer, but your answers are strings.

Just remove the parseInt and its braces.

          response = prompt(question);

Didnt test it but i think it should work.

christian bugay
christian bugay
4,260 Points

Thanks. But that doesn't seem to help. The counter still doesn't work.