Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Counter won't work please can someone look at my code?

var questionsAnswers = [ ["What was my first car registration?", "SANS"] , ["Where was I born?", "HULL"], ["What country did I last visit?", "DUBLIN"] ];

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

var score = 0;

for ( var i = 0; i < questionsAnswers.length; i += 1) { var myquestion = prompt(questionsAnswers[i][0]); if (myquestion.toUpperCase === questionsAnswers[i][1]); { score += 1000; print("Your answer was " + myquestion + "<p> The correct answer was " + questionsAnswers[i][1] + "</p>")

}

} print("Your score is " + score);

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

The issue isn't with your counter, you have a syntax error in the if condition. I'd also remove the semi-colon after the if condition (not needed).

2 Answers

It still doesn't work the score is adding 1000 each time the loop goes through the 3 question but I have only put a condition to add score of 1000 whenever the correct answer is given to the question.

var questionsAnswers = [ ["What was my first car registration?", "SANS"] , ["Where was I born?", "HULL"], ["What country did I last visit?", "DUBLIN"] ];

var myQuestion; var answers; var response;

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

var score = 0;

for ( var i = 0; i < questionsAnswers.length; i += 1) {

myQuestion = questionsAnswers [i][0]; answers = questionsAnswers [i][1]; response = prompt(questionsAnswers[i][0]); if (response.toUpperCase === answers[i][1]) { score += 1000; print("Your answer was " + myQuestion + "<p> The correct answer was " + answers + "</p>")

}

} print("Your score is " + score);

Aakash Srivastava
Aakash Srivastava
5,415 Points

HEY WAI SUNG ,

var questionsAnswers = [
    ["What was my first car registration?", "SANS"],
    ["Where was I born?", "HULL"],
    ["What country did I last visit?", "DUBLIN"]
];

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

var score = 0;

for (var i = 0; i < questionsAnswers.length; i += 1)  {  
    var myquestion = prompt(questionsAnswers[i][0])
    if (myquestion.toUpperCase === questionsAnswers[i][1]) ; {   // Remove the semicolon at the end and add parenthesis to the `toUpperCase()` function
        console.log(myquestion.toUpperCase());    
        score += 1000;
        print("Your answer was " + myquestion + "<p> The correct answer was " + questionsAnswers[i][1] + "</p>")
    }
}
print("Your score is " + score);

Due to that semicolon , you if condition is not running and it prints 3000 everytime. Just remove it. Also , you need to add the parenthesis to the toUpperCase() function in it's first occurance.
Now , you are good to go.

Thanks :)

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

That semi-colon isn't in the OP's code. Also your conditional expression is different, It is nice that you are showing the correction for the syntax error.

Aakash Srivastava
Aakash Srivastava
5,415 Points

Hey , @dave actually it was after the if block within the for loop , but by mistake , i showed it after for loop. Now updated the code. Thanks for pointing out :)
Sorry , due to those stickers in your name , i am unable to point your name explicitly.

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

No worries, might have to remove the stickers.

Back to this issue - the semi-colon shouldn't be there but doesn't really make a difference. The real issue is the syntax error in the if conditional expression.

Double check the if condition, I don't think there is any way to make it Truthy. Fix the condition and the script will work as desired.

Aakash Srivastava
Aakash Srivastava
5,415 Points

Sorry , but i didn't found any syntax error in the if condition.

May be I am unable to see the mistake or may be we are pointing to the same issue.

Also , I as per my knowledge , adding semicolon to the end of if statement makes JavaScript thinks that you have an empty statement. There are conditions where you can ignore semicolon in JavaScript.
But in this case , you can't ignore the semicolon , if it's there , all the statements inside the block will will considered emptly and always get executed no matter what . They will be treated as no longer belonging to the if conditional and thus independent of it.
Here is something that I want to share : https://news.codecademy.com/your-guide-to-semicolons-in-javascript/

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

Agreed on the semi-colon after the if. Retested and you are correct. Not sure what I messed up with my previous test.

The if condition still has a problem. I suggest you look a little closer.

Here a Pen. Test it out and put in console.log() checks on both items in the if condition if the error doesn't pop out.

Aakash Srivastava
Aakash Srivastava
5,415 Points

HEY , Dave, I found the issueactually .
Actually , we are working with two different versions of the code.
I am working with the first code he provies while asking the question . On ther other hand , you are working with the code ,he paste later in the answer section.
Sorry , these are different versions . In first section , he is only using the myquestion variable to store the question while in second version , he created another variable response .

Now coming to your point , you were right for second version of the code as his if condition is not right.
Thanks for sharing your pen :)