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

J V
J V
3,414 Points

Can someone help me understand what is wrong with storing correct values in my quiz program?

On question 3, I allow the user two chances to solve the question. The problem I am having is when the user answers the first attempt wrong, and the second attempt correctly, the score is not properly calculated at the end of the quiz. I am not sure how to fix or correct this error.

Also, I tried to post all of the HTML code, but it does not display properly for some reason.

Any help would be appreciated.

<!DOCTYPE html>
<html>

<head>
</head>

<body>

<h1>Geography & History</h1>
<p>Simple quiz game for you to play</p>

<script src = "script.js"></script>

</html>

Below is the JavaScript

/*********************************************************************

Geography and History Game

*********************************************************************/

/************************************************************
QUESTION #1
************************************************************/
var correct = 0;

var question1 = prompt("What year did Columbus sail across the Ocean?");
if(parseInt(question1) == 1492)
{
  correct = correct + 1;
}


/************************************************************
QUESTION #2
************************************************************/
var question2 = prompt("What is the longest river in the world?");
if(question2.toLowerCase() === "nile")
{
    correct = correct + 1;
}

/************************************************************
QUESTION #3
************************************************************/
var question3 = prompt("What is the biggest state in the USA?");

if (question3.toLowerCase() === "alaska")
{
  correct = correct + 1;
}
else if (question3.toLowerCase != "alaska")
{
  var question3again = prompt("Let's try again. What is the biggest state in the USA?");

  if(question3again.toLowerCase === "alaska")
  {
    correct = correct + 1;
  }
}
else
{
  alert("Sorry, you can not try again");
}

/************************************************************
STORING CORRECT VALUES
************************************************************/
/* === same type and value*/
if(correct === 3)
{
  document.write("Your score is " + correct + " out of 5." + "<br>" + "Award: Gold Crown");  
}
else if (correct === 2 || correct === 1)
{
  document.write("Your score is " + correct + " out of 5." + "<br>" + "Award: Silver Crown"); 
}
else 
{
  document.write("Your score is " + correct + " out of 5." + "<br>" + "Award: None"); 
}

2 Answers

Garrett Carver
Garrett Carver
14,681 Points

Hi Jaafar,
To answer your question, there are some places where you did not include the () at the end when you use the function .toLowerCase(). This is causing your if statements to not behave as expected.

It actually returns something weird when you don't include the parenthesis. Try running this code and you can see the difference:

var test = "TEST";
alert(test.toLowerCase);
alert(test.toLowerCase());

In addition, I think there may be another issue in your 2nd If statement for question 3. Your alert("Sorry, you can not try again"); will never fire. If you are still stuck, here's how I would have written it.

var question3 = prompt("What is the biggest state in the USA?");

if (question3.toLowerCase() === "alaska")
{
  correct = correct + 1;
}
else
{
  question3 = prompt("Let's try again. What is the biggest state in the USA?");

  if (question3.toLowerCase() === "alaska")
  {
    correct = correct + 1;
  }
}
else
{
  alert("Sorry, you can not try again");
}

Hope that helps! Let me know if you are still having trouble.

J V
J V
3,414 Points

Hi Garrett,

Thank you for answering my question. I appreciate your help.