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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

Kent Hefley
Kent Hefley
11,217 Points

I always get 4 out of 5 correct

I did something wrong here. I followed along with the video, but I can only get 4 out of 5 answers correct. I cannot seem to find my error? Can anyone else spot it?

// quiz begins. no answers correct
var correct = 0;

// ask questions
var answer1 = prompt("Name a programming language that is also a gem.");
if(answer1.toUpperCase() === "RUBY") {
correct += 1;
} 
var answer2 = prompt("Name a programming language that is also a snake");
if(answer2.toUpperCase() === "PYTHON") {
correct += 1;
}
var answer3 = prompt("What language do you use to style web pages?");
if(answer3.toUpperCase() === "CSS") {
correct += 1;
}
var answer4 = prompt("What language do you use to build the structure 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 got 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>Sorry, no crown for you. You need to study.</strong></p>");
  }

2 Answers

andren
andren
28,558 Points

The error is in the code for the last question:

var answer5 = prompt("What language do you use to add interactivity to a web page?");
if(answer5.toUpperCase === "JAVASCRIPT") {
correct += 1;
}

This is a relatively common mistake so it was not too difficult to spot. The issue is that you do not have parenthesis following the toUpperCase function call. In JavaScript if you type the name of a function without parenthesis then JavaScript will not execute the function, it will instead simply reference it. Which means that in your code you are asking JavaScript to compare toUpperCase (the function itself) against the string JAVASCRIPT. Instead of asking it to compare the result of calling toUpperCase against JAVASCRIPT, which is clearly what you intended to do.

If you add parenthesis like this:

var answer5 = prompt("What language do you use to add interactivity to a web page?");
if(answer5.toUpperCase() === "JAVASCRIPT") {
correct += 1;
}

Then your code will work.

Kent Hefley
Kent Hefley
11,217 Points

That did it. It's amazing how easy it is to overlook simple errors like that, even after staring at it for almost an hour. Thank you.

Todd Breeze
Todd Breeze
926 Points

I corrected the same thing but I allways get 4 out of 5 even after