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

When my code runs it says no answers were correct when I certainly answered all correct. What am I missing?

let correctNumber = 0;

// 2. Store the rank of a player let rank;

// 3. Select the <main> HTML element const main = document.querySelector('main');

/*

  1. Ask at least 5 questions
    • Store each answer in a variable
    • Keep track of the number of correct answers */ const guessA = prompt('How many planets are there?'); if ( guessA === 9 ){ correctNumber += 1; }

const guessB = prompt('How many primary colors are there?'); if ( guessB === 4 ){ correctNumber += 1; }

const guessC = prompt('How many colors are in the rainbow?'); if ( guessC === 7 ){ correctNumber += 1; }

const guessD = prompt('How many seasons are there?'); if ( guessD === 4 ){ correctNumber += 1; }

const guessE = prompt('How many teams play in the superbowl?'); if ( guessE === 2 ){ correctNumber += 1; }

/*

  1. Rank player based on number of correct answers
    • 5 correct = Gold
    • 3-4 correct = Silver
    • 1-2 correct = Bronze
    • 0 correct = No crown */ if ( correctNumber === 5 ) { rank = "Gold"; } else if ( correctNumber >= 3 ) { rank = "Silver";
      } else if ( correctNumber >= 1 ) { // check for 1-2 correct rank = "Bronze";
      } else { rank = "None :("; }

// 6. Output results to the <main> element main.innerHTML = <h2>You got ${correctNumber} out of five questions correct!</h2> <p>Crown earned: <strong>${rank}</strong></p> ;

2 Answers

Hi sonjajordan,

The reason you're being told that none of your answers were correct is because your answers are strings, but you're testing for integers.

The prompt function returns a string. So when you type in 4 as your answer to guessB, what's being returned from prompt and stored into guessB is not 4, it's '4'. So when you test is '4' === 4, the test fails.

You either need to test for the string version of the answers, or you need to convert the user's response to an integer. You can do this with the parseInt function (ie. const guessB = parseInt(prompt("How many primary colors are there? "))

Thank you! I hadn’t caught that very valuable piece of information.