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 Solution

Jan Chalupa
Jan Chalupa
3,323 Points

Why doesn't my if statement working?

Hi, so I just created this piece of code, but I can't figure out what's wrong with the if statement? Any good answer isn't added into var good and no +1 added to var score. All answers are added into var bad (even the right ones).

var questions = [
  ['Kolik je 2x2?', '4'],
  ['Kdo je prezident Bananove republiky?', 'zeman'],
  ['Ma zirafa ctyri nohy a dve hlavy?', 'ne']
];
var good = '<h1>Spravne odpovedi</h1>' + '<ol>';
var bad = '<h1>Spatne odpovedi</h1>' + '<ol>';
var window;
var answer;
var score = 0;

for ( i = 0; i < questions.length; i += 1) {
  window = prompt(questions[i][0]);
  answer = questions[i][1];
  if ( window === answer ) {
    good += '<li>' + questions[i][0] + '</li>';
    score += 1;
  } else {
    bad += '<li>' + questions[i][0] + '</li>';
  } 
}

good += '</ol>';
bad += '</ol>';

function print(rightQuestions, wrongQuestions) {
  document.write("tvoje score je: " + score);
  document.write(rightQuestions, wrongQuestions);
}

print(good, bad);

2 Answers

Brian Steele
Brian Steele
23,060 Points

Right away it looks like you're trying to store a variable with the name 'window', this is a reserved name in JS-try changing that

Jan Chalupa
Jan Chalupa
3,323 Points

Oh my god, thank you. I've spent a lots of time figuring this out!