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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
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.