Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Answers are not adding up correctly

Hi. The below code is not adding up the answers correctly as it always adds up to 5 no matter what answers are put in. Any idea why this is happening?

let correct = 0;

let rank;

const main = document.querySelector('main')

const answer1 = prompt('Name a programming language that is also a gem?'); if (answer1.toUpperCase() === 'RUBY'); {
  correct += 1;

const answer2 = prompt('Name a programming language that is also a snake?'); if (answer2.toUpperCase() ===  'PYTHON'); {
  correct += 1;

const answer3 = prompt('What programming language do you style web pages with?'); if (answer3.toUpperCase() === 'CSS'); {
  correct += 1;

const answer4 = prompt('What programming language do you use to build the structure of a website?'); if (answer4.toUpperCase() === 'HTML'); {
  correct += 1;

const answer5 = prompt('Which programming language can you use to build both the back end and the front end?'); if (answer5.toUpperCase() === 'JAVASCRIPT'); {
  correct += 1;

if (correct === 5 ) {
  rank = "Gold";
} else if ( correct >=3 ) {
  rank = "Silver";
} else if ( correct >=2 ) {
  rank = "Bronze";
} else {
  rank = "None :("

main.innerHTML = `<h2>You got ${correct} out of 5 questions.</h>
<p>Crowned earned: <strong>${rank}</strong></p>`

edited to add markdown

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, nazk! I received your request for assistance. I also took the liberty of adding some markdown to your code to make it easy to read. The answer to this lies in semicolons.

Here is a snippet from your code:

if (answer1.toUpperCase() === 'RUBY'); {
  correct += 1;

Remember that a semicolon ends a statement. So right now, it's ending the if statement then always adds +1 to the number of correct because it's not associated with that if statement. Try the following:

if (answer1.toUpperCase() === 'RUBY') { // removed the semicolon after the closing parenthesis
  correct += 1;

Note that you have this in multiple places. Hope this helps! :sparkles:

Jennifer Nordell got it working, thanks!

Hi Naz K -

You don't want to remove all semicolons, only the ones behind your if statements and between the ) and {.

This should help. So, for example, your first if statement would look like this:

const answer1 = prompt('Name a programming language that is also a gem?'); 
if (answer1.toUpperCase() === 'RUBY') {
  correct += 1;

Hope this helps!

Great, thanks for the help Robert Gulley, it works now.