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

quiz.js is not working the questions show up but when the right answers are giveing no pop saying you win or not help.

quiz.js is not working the questions show up but when give the right answers there is noting saying you win or you don't win.

var score = 0;
var totalQuestions = 3;

var question1 = prompt("Is D a dhampir?");
var question2 = prompt("Is D the son of a vampire father and human mother ?");
var question3 = prompt("Does D have a symbiotic left hand?");

var answer1 = "yes";
var answer2 = "yes";
var answer3 = "yes";

if(question1.toUpperCase() === answer1) {
    score +=1;
}
if(question2.toUpperCase() === answer2) {
    score +=1;
}
if(question3.toUpperCase() === answer3) {
    score +=1;
}

if(score === 3) {
  document.write("<p>You win .</p>");
} else if(score === 2) {
  document.write("<p>I'm sorry you didn't win.</p>");
}

Mod Note - Added backticks and a language tag for better code formatting.

4 Answers

There are a couple of issues that are preventing your code from printing the expected output.

var answer3 = "yes"; // Your answers are in all lowercase, but you set the user's response to be all uppercase.
if (question3.toUpperCase() === answer3) { // This won't ever be true.
}

if(score === 3) { 
  document.write("<p>You win .</p>"); // This code will run if they get all the answers correct.
} else if(score === 2) {
  document.write("<p>I'm sorry you didn't win.</p>"); // This code will run if they get two answers correct.
} // What happens if they get one or zero answers correct?

Hopefully that helps you fix your code. Let me know if you'd like some bigger hints.

+1 for Vampire Hunter D questions! Happy coding! :)

How this i think i get it fix.

var score = 0; var totalQuestions = 3;

var question1 = prompt("Is D a dhampir?"); var question2 = prompt("Is D the son of a vampire father and human mother ?"); var question3 = prompt("Does D have a symbiotic left hand?");

var answer1 = "Yes"; var answer2 = "Yes"; var answer3 = "Yes";

if(question1.toLowerCase() === answer1) { score +=1; } if(question2.toLowerCase() === answer2) { score +=1; } if(question3.toLowerCase() === answer3) { score +=1; }

if(score === 3) { document.write("<p>You win .</p>"); // This code will run if they get all the answers correct. } else if(score === 2) { document.write("<p>Not bad.</p>"); // This code will run if they get two answers correct. } else if(score === 1) { document.write("<p>Sorry you did not win.</p>"); }

There are still a couple of small issues.

You've changed your answer variables to have a capital letter "Yes", but you're now comparing it to the saved prompt converted to lowercase. This will still never be true. Let's take a look at the expression:

if (question1.toLowerCase() === answer1) { 
  // Assuming they answer correctly, this expression is currently equivalent to ("yes" === "Yes"), which is false.
  // You need to make sure that the correct answer will be the same as the answer you've defined.
}

Also, remember that if-else structures should always end with an else. What if they answer no questions correctly?

if(score === 3) { 
  document.write("<p>You win .</p>"); // This code will run if they get all the answers correct.
} else if(score === 2) {
  document.write("<p>Not bad.</p>"); // This code will run if they get two answers correct.
} else if(score === 1) { // This code will run if they get one answer correct.
  document.write("<p>Sorry you did not win.</p>"); 
} else {  // What if they get no answers correct?
  // Add something here that will print in case of all other possibilities.
}

Hopefully that helps out. Let me know if it's still not outputting correctly after you investigate these two things, and we'll walk through a solution. Happy coding!

i think i fix the else if what you think.

if(score === 3) { document.write("<p>You win .</p>"); // This code will run if they get all the answers correct. } else if(score === 2) { document.write("<p>Not bad.</p>"); // This code will run if they get two answers correct. } else if(score === 1) { document.write("<p>Sorry you only get one.</p>"); } else if(score ===0) {
document.write("<p>SORRY YOU LOSE!.</p>); }

Just redid my quiz it look ok .

var correct = 0; var totalQuestions = 3;

var answer1 = prompt("Is D a dhampir?"); if(answer1.toUpperCase() === 'YES') { correct +=1; }

var answer2 = prompt("Is D the son of a vampire father and human mother ?"); if(answer2.toUpperCase() === 'YES') { correct +=1; }

var answer3 = prompt("Does D have a symbiotic left hand?"); if(answer3.toUpperCase() === 'YES') { correct +=1; }

if(score === 3) { document.write("<p>You win .</p>"); } else if(score === 2) { document.write("<p>Not bad.</p>"); } else if(score === 1) { document.write("<p>Sorry you only get one.</p>"); } else if(score ===0) {
document.write("<p>SORRY YOU LOSE!.</p>); }