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 trialNoel Hale
1,715 PointsMy code not adding += score.
Need a bit of help with this code.
The score is not adding up and the document.write keeps reporting a 0 score even when answering correctly.
Here is the code....thanks in advance for any help:
var correct = 0;
var answer1 = prompt("Whats the capital of Ireland"); if (answer1.toUpperCase() === "Dublin"){ correct += 1; }
var answer2 = prompt("Whats the capital of America"); if (answer2.toUpperCase() === "New York"){ correct += 1; }
var answer3 = prompt("Whats the capital of England"); if (answer3.toUpperCase() === "London"){ correct += 1; }
var answer4 = prompt("Whats the capital of France"); if (answer4.toUpperCase() === "Paris"){ correct += 1; }
var answer5 = prompt("Whats the capital of Wales"); if (answer5.toUpperCase() === "Cardiff"){ correct += 1; }
document.write("<p>You guessed " + correct + " questions correctly.</p>");
2 Answers
Jacie Fortune
8,969 PointsYou are using toUpperCase() and so your answer string that you are comparing with needs to be all uppercase. For answer 1, you need to use DUBLIN in the comparison string, not Dublin. That should do the trick.
Nicholas Green
26,327 PointsJacie is right, you are using toUpperCase() but the string your comparing it to is in lowercase, except for the beginning. What would work is to change all the letters into an actual uppercase letter.
(answer1.toUpperCase() === "DUBLIN")
In JavaScript, the characters have to match exactly for it to run.