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

Ben Ahlander
Ben Ahlander
7,528 Points

Why are my correct answers still being marked wrong?

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

var questions = [
  ["Who is Bens favorite CEO?","ELON MUSK"],
  ["Why is 6 afraid of 7?","7 ate 9"],
  ["Is Ben smart?","YES"],
];

var rightAnswer = "";
var wrongAnswer = "";  

for (var i=0; i<questions.length; i+=1) {
  var answer = prompt(questions[i][0]);
  var questionNum = i + 1;

  if(answer[0].toUpperCase === questions[i][1]) {
    rightAnswer += questionNum +  questions[i][0] + "Your Answer = " + answer + ". Great Job!";
  } else {
    wrongAnswer += questionNum + questions[i][0] +
    "Your Answer = " + answer + "Correct Answer = " + questions[i][1];
  }
}

print("<h2><b>Questions Answered Correctly</b></h2>" + rightAnswer );
print("<h2><b>Questions Answered Incorrectly</b></h2>" + wrongAnswer);

[MOD: edited code block - srh]

1 Answer

Steven Parker
Steven Parker
231,269 Points

I see two issues:

  • you compare only the first character (answer[0]) instead of the whole answer
  • when you call toUpperCase(), you forgot the parentheses after the name