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 Add a Final Else Clause

Michael Gardiner
Michael Gardiner
4,282 Points

Personal Question

I am writing a code to take a quiz, but even when i enter the right answers it gives me "Wrong!" Can someone please help!?

script.js
/*
Ask at least 5 questions.
Keep track of how many questions the user got right.
Message how many s/he got right.
Rank player: 
    5 = gold. 3-4 = silver. 1-2 = bronze. 0 = none.
*/

var correct = 0

//Questions:
var numA = prompt("I am thinking of a number between 1 and 10. What is the number I am thinking of?");
if (numA === Math.floor(Math.random() * 10 ) + 1) {
    alert('Correct!');
    correct += 1;
} else {
    alert('Wrong!');
}

var colA = prompt("Is my favorite color purple or blue?");
if (colA.toUpperCase === "PURPLE") {
    alert('Correct!');
    correct += 1;
} else {
  alert('Wrong!');  
} 

var petA = prompt("Is my dog's name Logan Murphy or Spike?");
if (petA.toUpperCase === "LOGAN") {
    alert('Correct!');
    correct += 1;
} else {
    alert('Wrong!');
}

var ageA = prompt("How old am I?");
if (ageA === 18) {
    alert('Correct!');
    correct += 1;
} else {
    alert('Wrong!');
} 

var liveA = prompt("Where do I live?");
if (liveA.toUpperCase === "NEW YORK CITY" || liveA.toUpperCase === "NEW YORK" || liveA.toUpperCase === "NYC") {
    alert('Correct!');
    correct += 1;
} else {
    alert('Wrong!');
} 

//Results:
document.write("You got " + correct + " out of 5 questions correct. ");

//Ranking:
if (correct === 5) {
    document.write("You earned a gold crown!");
} else if (correct >= 3) {
    document.write("You earned a silver crown!");
} else if (correct >= 1) {
    document.write("You earned a bronze crown!");
} else {
    document.write("No crown for you! Try again!");
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/main.css">
  <title>Time Calculator</title>
 </head>
<body>
  <div class="container">
  <h1>Time calculator</h1>
  <script src="script.js"></script>
  </div>
</body>
</html>

2 Answers

Rod Barbee
Rod Barbee
11,935 Points

Each line needs to end with a semi-colon. Look at the first line.

Rod Barbee
Rod Barbee
11,935 Points

the first uncommented line, that is

Michael Gardiner
Michael Gardiner
4,282 Points

I fixed that, still says Wrong for every answer