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

What is wrong with my code?

let correct = 0; let rank; const main = document.querySelector('main');

questionOne = prompt("What is 1+1?"); if (questionOne === 2) { correct += 1; }

questionTwo = prompt("What is 2+2?"); if (questionTwo === 4) { correct += 1; }

questionThree = prompt("What is 3+3?"); if (questionThree === 6) { correct += 1; }

questionFour = prompt("What is 4+4?"); if (questionFour === 2) { correct += 1; }

questionFive = prompt("What is 5+5?"); if (questionFive === 10) { correct += 1; }

if (correct === 5) { rank = 'Gold'; } else if (correct >= 3) { rank = 'Silver'; } else if (correct >= 1) { rank = 'Bronze'; } else { rank = 'No crown'; }

main.html = <h2>You got ${correct} correct. Your rank is ${rank}</h2>.;

2 Answers

Hi Mattew!

There are several issues here.

First, prompt returns a string. So (questionOne === 2) is false.

You either need to change 2 to '2' (a string) or convert questionOne to an integer using parseInt.

Second, you need backticks around your HTML string (being a template literal).

Third, your need to use main.innerHTML.

This should work:

let correct = 0; let rank;
const main = document.querySelector('main');

questionOne = prompt("What is 1+1?"); if (questionOne === '2') { correct += 1; }

questionTwo = prompt("What is 2+2?"); if (questionTwo === '4') { correct += 1; }

questionThree = prompt("What is 3+3?"); if (questionThree === '6') { correct += 1; }

questionFour = prompt("What is 4+4?"); if (questionFour === '8') { correct += 1; }

questionFive = prompt("What is 5+5?"); if (questionFive === '10') { correct += 1; }

if (correct === 5) { rank = 'Gold'; } else if (correct >= 3) { rank = 'Silver'; } else if (correct >= 1) { rank = 'Bronze'; } else { rank = 'No crown'; }

main.innerHTML = `<h2>You got ${correct} correct. Your rank is ${rank}</h2>.`;

BTW, you can test it here:

https://www.w3schools.com/js/tryit.asp?filename=tryjs_array

Using this code:

<!DOCTYPE html>
<html>
<body>

<main>
</main>

<script>
let correct = 0; let rank;
const main = document.querySelector('main');

questionOne = prompt("What is 1+1?"); if (questionOne === '2') { correct += 1; }

questionTwo = prompt("What is 2+2?"); if (questionTwo === '4') { correct += 1; }

questionThree = prompt("What is 3+3?"); if (questionThree === '6') { correct += 1; }

questionFour = prompt("What is 4+4?"); if (questionFour === '8') { correct += 1; }

questionFive = prompt("What is 5+5?"); if (questionFive === '10') { correct += 1; }

if (correct === 5) { rank = 'Gold'; } else if (correct >= 3) { rank = 'Silver'; } else if (correct >= 1) { rank = 'Bronze'; } else { rank = 'No crown'; }

main.innerHTML = `<h2>You got ${correct} correct. Your rank is ${rank}</h2>.`;
</script>

</body>
</html>

(Cut and paste the HTML code into the left pane - replacing everything. Then run it.)

I hope that helps.

Stay safe and happy coding!

Thank you Peter!