JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Thomas Scott
Thomas Scott
7,060 Points

Not sure what I've done wrong, keep getting 0 out of 5.

/*

  1. Store correct answers
    • When quiz begins, no answers are correct */

const correctAnswer1 = 'FOOD'; const correctAnswer2 = 'AUSTRALIA'; const correctAnswer3 = 'DOG'; const correctAnswer4 = 'CAT'; const correctAnswer5 = 'PHONE';

// 2. Store the rank of a player var rank = 0; var result;

// 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 answer1 = prompt('what do humans eat?'); const answer2 = prompt('what country do you live in?'); const answer3 = prompt('what is your favorite animal?'); const answer4 = prompt('what is your least favorite animal?'); const answer5 = prompt('what could you not live without?');

if(answer1.toUpperCase === correctAnswer1){ rank += 1; } if(answer2.toUpperCase === correctAnswer2){ rank += 1; } if(answer3.toUpperCase === correctAnswer3){ rank += 1; } if(answer4.toUpperCase === correctAnswer4){ rank += 1; } if(answer5.toUpperCase === correctAnswer5){ rank += 1; }

//console.log(rank); /*

  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( rank == 5){ result = 'gold'; } else if(rank >= 3 && rank < 5 ){ result = 'Silver'; } else if (rank >= 1 && rank < 3) { result = 'Bronze'; } else if (rank == 0){ result = 'No Crown'; }

const mainresult = <h1>Your result is ${result}!</h2>;

// 6. Output results to the <main> element

main.innerHTML = mainresult;

1 Answer

Hi Thomas!

You had some typing errors and you were commenting-out some essential lines of code.

You biggest issue, though, is .toUpperCase is a function/method, not an attribute/property, so you need to use (), like this:

answer.toUpperCase() === correctAnswer

This should work:

/*

Store correct answers
When quiz begins, no answers are correct */
const correctAnswer1 = 'FOOD';
const correctAnswer2 = 'AUSTRALIA';
const correctAnswer3 = 'DOG';
const correctAnswer4 = 'CAT';
const correctAnswer5 = 'PHONE';

// 2. Store the rank of a player
var rank = 0; var result;

// 3. Select the <main> HTML element
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 do humans eat?');
console.log(answer1);
console.log(correctAnswer1);
const answer2 = prompt('what country do you live in?');
console.log(answer2);
console.log(correctAnswer2);
const answer3 = prompt('what is your favorite animal?');
console.log(answer3);
console.log(correctAnswer3);
const answer4 = prompt('what is your least favorite animal?');
console.log(answer4);
console.log(correctAnswer4);
const answer5 = prompt('what could you not live without?');
console.log(answer5);
console.log(correctAnswer5);

if(answer1.toUpperCase() === correctAnswer1){ rank += 1; }
console.log(rank);
if(answer2.toUpperCase() === correctAnswer2){ rank += 1; }
console.log(rank);
if(answer3.toUpperCase() === correctAnswer3){ rank += 1; }
console.log(rank);
if(answer4.toUpperCase() === correctAnswer4){ rank += 1; }
console.log(rank);
if(answer5.toUpperCase() === correctAnswer5){ rank += 1; }
console.log(rank);

//console.log(rank);

/*
Rank player based on number of correct answers
5 correct = Gold
3-4 correct = Silver
1-2 correct = Bronze
0 correct = No crown */
if( rank == 5){
    result = 'gold';
} else if(rank >= 3 && rank < 5 ){
    result = 'Silver';
} else if (rank >= 1 && rank < 3) {
    result = 'Bronze';
} else if (rank == 0){
    result = 'No Crown'; }
const mainresult = `<h1>Your result is ${result}!</h2>`;

// 6. Output results to the <main> element

main.innerHTML = mainresult;

Notice all the console.log statements I added, to help me spot the errors.

Also, your "answer" (mainresult), being a template literal, needed to be surrounded by backticks.

I hope that helps.

Stay safe and happy coding!