Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Daniel Pawlowski
Daniel Pawlowski
3,586 Points

JavaScript 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

// 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
Daniel Pawlowski
3,586 Points

Thanks, Tobias!!

I see the score works now. Can you explain what was causing my original code to not increment the score??

toUppercase() 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 :)

Daniel Pawlowski
Daniel Pawlowski
3,586 Points

Ahh!!!! 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