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 trialDaniel Pawlowski
3,586 PointsJavaScript wont increment my score correctly
Hello!
I'm doing a code challenge and I cannot figure out why my score is always zero. My score wont increment even though the question is correct.
// quiz begins, no score
var score = 0;
// ask questions
var ans1 = prompt("What's the name of the hero ship in Star Trek?");
var ans2 = prompt("Who commanded the NCC-1701-D?");
var ans3 = prompt("Your favorite android's name?");
var ans4 = prompt("Qu'pla! Name the language?");
var ans5 = prompt("The neutral zone borders this empire?");
// compare the answers
if ( ans1.toUpperCase === 'ENTERPRISE' ) {
score += 1;
}
if ( ans2.toUpperCase === 'PICARD' ) {
score += 1;
}
if ( ans3.toUpperCase === 'DATA' ) {
score += 1;
}
if ( ans4.toUpperCase === 'KLINGON' ) {
score += 1;
}
if ( ans5.toUpperCase === 'ROMULAN' ) {
score += 1;
}
// calculate score and display
document.write('<p>Congratulations! You scored ' + score + ' points!</p>');
2 Answers
tobiaskrause
9,160 Points// quiz begins, no score
var score = 0;
// ask questions
var ans = "";
ans = prompt("What's the name of the hero ship in Star Trek?");
if ( ans.toUpperCase() === 'ENTERPRISE' ) {
score += 1;
}
ans = prompt("Who commanded the NCC-1701-D?");
if ( ans.toUpperCase() === 'PICARD' ) {
score += 1;
}
ans = prompt("Your favorite android's name?");
if ( ans.toUpperCase() === 'DATA' ) {
score += 1;
}
ans = prompt("Qu'pla! Name the language?");
if ( ans.toUpperCase() === 'KLINGON' ) {
score += 1;
}
ans = prompt("The neutral zone borders this empire?");
if ( ans.toUpperCase() === 'ROMULAN' ) {
score += 1;
}
// calculate score and display
document.write('<p>Congratulations! You scored ' + score + ' points!</p>');
I changed your code a little bit :) this way you only need one variable ans not five. Nice questions btw
Daniel Pawlowski
3,586 PointsAhh!!!! I understand perfectly!
The crazy thing is, the Treehouse video explaining how they did the challenge didn't use .toUpperCase() either. Oh well! I'm glad you cleared it up.
-Dan
Daniel Pawlowski
3,586 PointsDaniel Pawlowski
3,586 PointsThanks, Tobias!!
I see the score works now. Can you explain what was causing my original code to not increment the score??
tobiaskrause
9,160 Pointstobiaskrause
9,160 PointstoUppercase() is a method http://www.w3schools.com/jsref/jsref_touppercase.asp
a method always has brackets at the end...even when there are no parameters for the method. You didn't made this brackets so toUpperCase didn't work. So none of your condition had the value true (since all the answers were uppercase).
I hope you can understand...my english is not that great :)