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 trial
Elizabeth Eymann
12,209 PointsThe Ultimate Quiz Challenge - JavaScript
Hi everyone,
I followed Dave's example in building the "Ultimate Quiz Challenge" using JavaScript. I followed it code for code. When I preview my work in workspace, everything appears to be good. I follow the prompts, entering my answers. Then, at the end I get a message saying I only got one answer right! But that's not correct. I know I got all of them right! I looked and looked...I can't see what is wrong with my code...Here is is...Can anyone help?
Here is a screenshot of my workspace: https://w.trhou.se/g4n4vi05ix
/*
Quiz:
- ask 5 questions
- keep track of number correct questions
- provide a final message letting the user know how many answers they got correct.
- rank the player: all 5= gold crown 3-4 = silver crown 1-2 = bronze crown 0 = no crown
```JavaScript /* quiz begins, no answers correct */ var correct = 0;
/* ask questions */
var answer1 = prompt("Name a programming language that's also a gem");
if ( answer1.toUpperCase() === 'RUBY') {
correct += 1;
}
var answer2 = prompt("Name a programming language that's also a snake");
if (answer2.toUpperCase === 'PYTHON') {
correct += 1;
}
var answer3 = prompt("What language do you use to syle web pages?");
if (answer3.toUpperCase === 'CSS') {
correct += 1;
}
var answer4 = prompt("What language do you use to build the structre of web pages?");
if (answer4.toUpperCase === 'HTML') {
correct += 1;
}
var answer5 = prompt("What language do you use to add interactivity to a web page?");
if (answer5.toUpperCase === 'JAVASCRIPT') {
correct += 1;
}
// output results document.write("<p>You got " + correct + " out of 5 questions correct.</p>");
// output rank if ( correct === 5) { document.write("<p><strong>You earned a gold crown!</strong></p>"); } else if ( correct >= 3 ) { document.write("<p><strong>You earned a silver crown!</strong></p>"); } else if ( correct >= 1) { document.write("<p><strong>You earned a bronze crown!</strong></p>"); } else { document.write("<p><strong>No crown for you. You need to study!</strong></p>"); }```
2 Answers
andren
28,558 PointsThis is a very common issue which is caused by a pretty small but Impactful typo.
Look at the comparison for your first question:
answer1.toUpperCase() === 'RUBY'
And compare it to the comparison for the second question:
answer2.toUpperCase === 'PYTHON')
Can you see any difference between them beyond the answer number and question? Well there is in fact a difference. In the first question you have parentheses following the toUpperCase method, in the second question you do not.
In JavaScript if you don't use parentheses after a method you will in fact not execute the method, you will instead just create a reference to the method. So in your second condition you are asking JavaScript to compare toUpperCase (the method itself) against 'Python', rather than comparing the result of calling toUpperCase on the answer like you intended to do.
If you simply add parentheses to the comparison for all of your questions then your code will work fine.
Elizabeth Eymann
12,209 PointsThank you, Andren!