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

Larissa Röhrig
Larissa Röhrig
1,787 Points

I really need help with my true and false questions!

My true and false questions aren't working the way I want.... I get the answer "Sorry that is not right" when I wanted that the message "Your answer is correct" appears.

//Beginning of my game, asking five questions
// true or false questions
// Quiz Books
alert("This little quiz will test your knowledge about books.Please answer only with true or false!");
// Keeping track of the correctly answered questions
var questionOne = prompt("Is 'Me before you' a book and a movie?");
var questionOne= true ;
if (true) {
alert("Your answer is correct.");
}
else {
alert("Sorry but that is not right");
}
var questionTwo = prompt("Is John Green the author of 'The fault in our stars'?");
var questionTwo= true;
if (true) {
alert("Your answer is correct");
}
else {
alert("Sorry but that is not right");
}
var questionThree = prompt("Was Marcel Reich-Ranicki just an auhtor?")
var questionThree= true;
if (false) {
alert("Your answer is correct");
}
else {
alert("Sorry but that is not right");
}
var questionFour = prompt("Is Hamlet a book?");
var questionFour= true;
if (true) {
alert("Your answer is correct");
}
else {
alert("Sorry but that is not right");
}
var questionFive = prompt("Pride and Prejudice is written by Shakespeare?");
var questionFive= false;
if (false) {
alert("Your answer is correct");
}
else {
alert("Sorry but that is not right");
}

2 Answers

Hi, here are a few things that I notice about the posted code. I'll use the first question as an example.

var questionOne = prompt("Is 'Me before you' a book and a movie?");   // this prompts the user for an answer
var answerOne = questionOne.toLowerCase() === 'true' ;   // this converts answer to lower case letters and tests if the answer matches 'true'
if (answerOne) {   // if it is true
    alert("Your answer is correct.");   // display this 
} else {   // otherwise 
    alert("Sorry but that is not right");   // display this
}

I hope this helps.

Larissa Röhrig
Larissa Röhrig
1,787 Points

Thank you! That helped a lot :)

Steven Parker
Steven Parker
231,236 Points

Let's take a look at one of the questions:

var questionFive = prompt("Pride and Prejudice is written by Shakespeare?");
var questionFive= false;
if (false) {

On the first line, you present the user with a question and get the response in the variable questionFive. So far, so good.

On the second line, you redeclare the variable and force the answer to be false. You should never redeclae a variable (but you can always reassign it), and setting it to false only wipes out the answer that was given. So this entire line should not be here.

Finally, on line three, you check the value of false. Not surprisingly, its value is false, so the "correct" feedback will never be shown. Instead, the else condition will present the "sorry" feedback. What you really want here is to check if the answer given is correct, so you have to compare it to something. For this question, you probably want to see if the user typed "false", so the line should be:

if (questionFive == "false") {

You could make it a bit more sophisticated and check for either "false" or "no", and also be able to handle a user with caps lock turned on:

if (questionFive.toLowerCase() == "false" || questionFive.toLowerCase() == "no") {

Since each of the five questions is structured similarly, they will all need the same fix-ups.

Larissa Röhrig
Larissa Röhrig
1,787 Points

Thank you for your fast and detailed help! That helped a lot! Now I just need to find the best way to keep track of the correctly answered questions ;)