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 trialNaz K
Full Stack JavaScript Techdegree Student 10,163 PointsAnswers 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
Treehouse TeacherHi 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!
Robert Gulley
Front End Web Development Techdegree Student 10,722 PointsHi 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!
Naz K
Full Stack JavaScript Techdegree Student 10,163 PointsGreat, thanks for the help Robert Gulley, it works now.
Naz K
Full Stack JavaScript Techdegree Student 10,163 PointsNaz K
Full Stack JavaScript Techdegree Student 10,163 PointsJennifer Nordell got it working, thanks!