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 trialTara K
1,863 PointsCode is not calculating number of correct answers appropriately.
Greetings,
My code continues to calculate the score as only 2 correct/bronze, even though I am inputting all 5 correct answers. Any idea why it isn't adding up the correct answers appropriately?
let correctAnswers = 0;
let playerRank;
const main = document.querySelector("main");
/*
- Ask at least 5 questions
- Store each answer in a variable
- Keep track of the number of correct answers */
const answer1 = prompt("What are female elephants called?");
if ( answer1.toUpperCase() === "COWS" ) {
correctAnswers += 1;
}
const answer2 = prompt("What is the tallest animal in the world?");
if ( answer2.toUpperCase() === "GIRAFFE" ) {
correctAnswers += 1;
}
const answer3 = prompt("Baby rabbits are born blind, true or false?");
if ( answer3.toUpperCase === "TRUE" ){
correctAnswers += 1;
}
const answer4 = prompt("What food makes up nearly all of a Giant Panda's diet?");
if ( answer4.toUpperCase === "BAMBOO" ){
correctAnswers += 1;
}
const answer5 = prompt("What is the largest type of 'big cat' in the world?");
if ( answer5.toUpperCase === "TIGER" ){
correctAnswers += 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 (correctAnswers === 5) { playerRank = "Gold"; } else if (correctAnswers >= 3) { playerRank = "Silver"; } else if (correctAnswers >= 1) { playerRank = "Bronze"; } else { playerRank = "No crown"; }
// 6. Output results to the <main> element
main.innerHTML =
<h2>You got ${correctAnswers} out of 5 questions correct.</h2>
<p>Crown earned: <strong>${playerRank}</strong></p>
;
1 Answer
Steven Parker
231,248 PointsWhen calling a method, you must always put parentheses after the name, whether or not it takes arguments. So the cases where "toUpperCase" has no parentheses will return the function itself and not the result of running it, and will never match the expected answer.
Tara K
1,863 PointsTara K
1,863 PointsOh jeez! I can't believe I missed that. Thank you!!