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 trialLorraine Wheat
6,083 PointsWhy I only getting the answer for true?
In my below code, when I run the question, I only get it to print true. Am I doing something wrong?
var abe = false;
var question = prompt('Who is the President who freed the slaves?');
var answer = 'Abraham Lincoln';
if (question.toUpperCase === answer.toUpperCase ) {
abe = true;
}
if (abe) {
document.write('<p>You\'re right!</p>');
} else {
document.write('<p>Nope! You\'re wrong!</p>');
}
4 Answers
Joshua Britton
10,252 PointsI know the other posters already answered the question, but wanted to add the reason it was always evaluating to true. Without the parentheses after the functions, the browser was comparing the "types" of the .toUpperCase() and .toLowerCase() methods rather than comparing the answers stored in the "question" and "answer" objects - .toUpperCase and .toLowerCase are both "functions", so the browser was asking is "function" === "function" which evaluates to true.
You can test this for yourself by opening the Developer Tools console and typing the following: question.toUpperCase === answer.toUpperCase
After hitting enter, the console evaluates this expression to 'true' (regardless of whether the value in question is identical to the value in answer). To see why that is, type the following on the next console line: typeof(question.toUpperCase)
The word "function" will appear after hitting enter. Next type the following on the next console line: typeof(answer.toUpperCase)
After hitting enter, you'll see the word "function" again. So, the expression in the original if statement was asking: Is "function" === "function" (which of course, is true).
I'm still learning JavaScript so I might be wrong, but I think that's what was going on. If I'm wrong, someone please correct me.
Jason Anders
Treehouse Moderator 145,860 PointsHey Lorraine,
The problem lies within your first if statement
. The toUpperCase
is a method that you are calling, and therefore requires the ()
after the method name. So, you're really close, you just need to add those and the code will work.
if (question.toUpperCase() === answer.toUpperCase() ) {
Keep Coding!
Lorraine Wheat
6,083 PointsThanks a lot!
Lorraine Wheat
6,083 PointsThanks a lot!
jason chan
31,009 Pointsvar question = prompt('Who is the President who freed the slaves?');
var answer = 'Abraham Lincoln';
if (question.toUpperCase() === answer.toUpperCase() ) {
document.write('<p>You\'re right!</p>');
} else {
document.write('<p>Nope! You\'re wrong!</p>');
}
Done
Lorraine Wheat
6,083 PointsThanks a lot
jason chan
31,009 Pointsvar question = prompt('who is the first president who freed the slaves ?');
var answer = "lincoln";
if(question === answer) {
alert('correct')
} else {
alert('wrong')
}
should be something like this.
Lorraine Wheat
6,083 PointsLorraine Wheat
6,083 PointsThat is very helpful, providing the reasoning behind it. Now I know that I was evaluating the type.