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 trialMatthew Miller
3,522 PointsWhat 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
Peter Vann
36,427 PointsHi 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!
Matthew Miller
3,522 PointsThank you Peter!