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 Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

rocky roy
rocky roy
556 Points

Why this code is not working properly it's showing some unexpected result.

var RightAnswer = 0; // For storing the right answer
var WrongAnswer = 0; // For wrong the right answer
var StoreAnswer = false;
var question1 = prompt('the capital of india ?');
if (question1.toUpperCase() === 'DELHI') {
    StoreAnswer = true;
} if (StoreAnswer === true) {
    RightAnswer += 1;
} else if (StoreAnswer === false) {
    WrongAnswer += 1;
}
// End Question

var question2 = prompt('the capital of West Bengal ?');
if (question2.toUpperCase() === 'KOLKATA' || question2.toLowerCase() === 'siliguri') {
    StoreAnswer = true;
} if (StoreAnswer === true) {
    RightAnswer += 1;
} else if (StoreAnswer === false) {
    WrongAnswer += 1;
}
// End Question

var question3 = prompt('Tell me a goode name ?');
if (question3.toLowerCase() === 'jesus' || question3.toLowerCase() === 'krisna') {
    StoreAnswer = true;
} if (StoreAnswer === true) {
    RightAnswer += 1;
} else if (StoreAnswer === false) {
    WrongAnswer += 1;
}
// End Question
document.getElementById('demo').innerHTML= 'The number of time you attened wrong & right is <b> '+ RightAnswer.toString() + '</b> right & <b>' + WrongAnswer.toString() + '</b> wrong ';
Aaron Martone
Aaron Martone
3,290 Points

I'm not sure I understand, but if you want to show true/false based on the numeric value, where 0 = false and anything else is true, simply add a '!!' to the beginning of the variable. For example if a variable = 2, then !!variable is saying: 1. What is !variable? Well, 2 == true, so !true = false. And then the second '!' kicks in and says "What is !false?" which is true. Basically it's a way of forcing a variable to display true/false via coercion. 0, -0, NaN, '', undefined and null are all considered falsy values. Anything else will resolve to true.

2 Answers

Steven Parker
Steven Parker
231,275 Points

Your program uses an intermediate variable StoreAnsswer in determining the correctness of an answer, but once a correct answer is given, it never gets reset. This variable isn't really needed since you can tally the results when you test the answer. An example of doing it this way with the first question would look like this:

var question1 = prompt('the capital of india ?');
if (question1.toUpperCase() === 'DELHI') {
    RightAnswer += 1;
} else {
    WrongAnswer += 1;
} // End Question

If you modify all questions to work like that you will get the correct totals.

rocky roy
rocky roy
556 Points

That i also know but i wants also capture the true and false that's why i used StoreAnsswer variable . you can see on my code that . document.getElementById('demo').innerHTML= 'The number of time you attened wrong & right is <b> '+ RightAnswer.toString() + '</b> right & <b>' + WrongAnswer.toString() + '</b> wrong ';i i also want to show true and false.

if you want to display the number of right and wrong answers you can display both variables RightAnswer and WrongAnswer. This will give you the true and false.