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

renedoukhan
renedoukhan
4,761 Points

Why is my code wrong ?

Even when the response given is wrong, the program states 'Right'. Why ?

var quizz = [ ['Date de la fête nationales française ?','14 juillet'], ['Prénom du président français actuel ?','François'], ['Capitale de la France','Paris'], ['Dans quel continent se situe la France ?','Europe'] ]; var p=0

for (i=0; i<=quizz.length;i++){ var question = prompt (quizz [i][0]); if (question == quizz [i][1]){ document.write ('Right'); p++; }else { alert('Wrong'); } } document.write ('You have ' + p + 'right anwers');

Thank you !

3 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hi Rene, your code executes fine for me. When I get a question wrong an alert comes up saying "Wrong." and when I get a question right, "Right" is printed to the screen. However I noticed that every time I get a question right it prints to the screen so I end up seeing "RightRightRight" after I complete the quiz, so maybe that's why you think your code is not working??

renedoukhan
renedoukhan
4,761 Points

Thanks Grace

Well, it's working now. I don't know what happened. There's just one thing that is still not working : that part document.write ('You have ' + p + 'right anwers'); That sentence doesn't show on the document.

Err, is your name really GRace Kelly :) Best

Grace Kelly
Grace Kelly
33,990 Points

Hmm I would try changing your for loop to say less than rather than less than/equal to, try changing it to this:

for (i=0; i<quizz.length;i++){ //change <= to <
  var question = prompt (quizz [i][0]); 
  if (question == quizz [i][1]){ 
    document.write ('Right'); p++; 
  }else { 
    alert('Wrong'); 
  } 
} 

hope that helps and yep that's my name! :)

renedoukhan
renedoukhan
4,761 Points

It does. I understand my mistake now. Thanks !