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 Introducing Conditional Statements

Abir Ahmed
PLUS
Abir Ahmed
Courses Plus Student 8,616 Points

Why we have to write specifically "RUBY" in our program when using .toUpperCase method?

In the workspace I tried to write:

if (answer.toUpperCase() === "ruby")

but seems like it is not working, then I followed the tutor and wrote this:

if (answer.toUpperCase() === "RUBY")

and now the program works.

My question is why we have to write specifically RUBY in capital form, I don't want answer to be like this is because I have used method .toUpperCase() and if I would've use .toLowerCase() then I could have wrote ruby, and that doesn't answer my question and my question remain same again that why we have to write specifically RUBY or ruby, can you please give in depth answer, thank you very much :)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there Abir Ahmed! Let's take a look at this line:

if (answer.toUpperCase() === "ruby")

What we're seeing here is a variable named answer. Now we can assume that this holds the answer input by the user. But let's suppose for a moment that the .toUpperCase part isn't there and we have this:

if (answer === "ruby")

If the user enters RUBY, or Ruby or ruBY or any other combination they will be counted as incorrect because it's not identical to the answer "ruby.". So the toUpperCase is going to take that answer and put it in all uppercase. So let's say the user enters "ruby" or "Java". Those will be changed to "RUBY" and "JAVA" respectively. So no matter what they enter, the all uppercase is never going to be equal to "ruby". The first piece of code will fail every time.

However, you could do it like this:

if(answer.toLowerCase()  === "ruby")

Now, if the user enters "RuBy" or "Ruby" or "JAVASCRIPT" those will be changed to "ruby" "ruby" and "javascript" respectively. Now it's possible for their answer to be identical to "ruby". Hope this helps! :smiley: