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 trialVictor Horton
20,277 PointsWhy won't this add up my score?
var correct = 0;
var question1 = prompt("Who is the president?"); if ( question1.toUpperCase === 'OBAMA') { correct += 1; } var question2 = prompt("What is the month?"); if (question2.toUpperCase === 'SEPTEMBER') { correct += 1; } var question3 = prompt("Whos is the best?"); if ( question3.toUpperCase === "VICTOR") {correct += 1; } var question4 = prompt("What month was I born?"); if ( question4.toUpperCase === 'NOVEMBER' ) { correct += 1; } var question5 = prompt("What do I do?"); if ( question5.toUpperCase === 'SALES') { correct += 1; }
// output results document.write("<p>You got " + correct + " out of 5</p>");
//output rank
if ( correct === 5) { document.write("<p>You got a Gold Star!</p>"); } else if ( correct >= 3) { document.write("<p> You got a Silver Star!</p>"); } else if ( correct >= 1) { document.write("<p> You got a Bronze Star</p>"); } else { document.write("<p> Sorry you suck </p>"); }
1 Answer
andren
28,558 PointsThe problem with your code is your call to the toUpperCase function, or namely the fact that you are not actually calling it, only referencing it.
In JavaScript (and most other modern programming languages) you call functions by adding parentheses after the function name, like this: toUpperCase(), not adding those parentheses in JavaScript means that you are simply referencing the function rather than executing it. Which means that in your if statements you are actually comparing the function toUpperCase itself to the answers rather than the input that was prompted for.
Victor Horton
20,277 PointsVictor Horton
20,277 PointsThank you. great explanation.