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 Review Conditional Statements and Comparison Operators

How can ( 'lion' > 'zebra ') be false when ( 'apple '> 'bear ') is true? they are the same as L is before Z and A>B.

I believe one of the questions answers on the quiz has the incorrect answer saved as the correct answer. Earlier on the video its stated that apple> bear is true because 'a' comes before 'b' in the alphabet; however during the quiz its said that 'lion' > 'zebra' is false when I selected true based on 'L' coming before 'z' in the alphabet. Please inform me if I am understanding this incorrectly.

3 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

Ok, well first of all I want to correct you, in the video he doesn't say that 'apple' > 'bear' he says that 'apple' < 'bear'.


Now onto the meat and potatoes of this, now bear (haha, get it) with me on this one there is going to be some math.


Background Info

JavaScript represents characters as an actual unicode (basically a number) in this case JavaScript utilizes the UTF-16 character encoding. Basically, encoding is a way for a computer to represent a value via a different value. In this case it is represented as either a hex value or a decimal value.

Hex is a base-16 number, so we see that is has values ranging from 0 - 9 and A - F: x00F9A3 would be a hexadecimal number.

A decimal is a base-10 number and ranges from 0 - 9, and is what we are used to seeing: The decimal version of x00F9A3 is 63907. I won't go into detail on how to convert the numbers, as it isn't important here, it is just some background.


Now that you know a little bit about the math, let's explain why you aren't getting what you are expecting.

When JavaScript sees a String, it doesn't actually see the alphabet. The letters are basically for human readability but JavaScript's compiler doesn't actually understand any of it. Thus it wouldn't be able to actually determine if 'A' comes before 'B' or not.

To alleviate this, the developers of basically all programming languages, have the language convert the String into a series of numbers, that the computer can understand. In JavaScript's case it converts the String into a code that it can read via UTF-16 character encoding.

Thus when you type in the word 'apple', JavaScript actually sees:| x61 | x70 | x70 | x6C | x65 | in hex. Converted to decimal it looks like this: | 97 | 112 | 112 | 108 | 101 |. I think you can figure out what letter each number here represents.

Likewise bear would be represented as: | x62 | x65 | x61 | x72 | or | 98 | 101 | 97 | 114 | in decimal.

If you haven't already guessed: a = x61 or 97 and b = x62 or 98.


Thus when JavaScript is comparing the a in apple with the b in bear. It is actually saying: 97 > 98 which is False.


Note that I have a degree in computer science so that is why I know all of this stuff. If it has gone over your head, let me know and I will try to explain it in easier terms.

Awesome information, thanks! I was trying to locate the part of the video so I could verify but after not finding it I just posted the question based on memory. It sure makes more sense being that it was opposite of what I thought I saw. Yes some of the historical and number conversions that you spoke of did go over my head, like why is A-97 versus 100 or 1, but Im guessing it has to do with the other characters that can be used in the language. Like when he said that a number is worth less than a letter(unless I have that backwards too lol); but as expected I have more questions on those topics but I will try to find the answers else where versus tying up your time when others may need you here, Thanks for your thorough and detailed response to my inquiry.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

Don't worry about tying up my time, if you have questions you want me to reiterate on or explain in further detail ask away: Make me earn that degree, lol!.

As for the why the value of a is 97, that is just how UTF-16 decodes the value. Generally in Unicode a is represented as the value 97. Don't worry too much about the math of how the letter is converted, it's just important that you understand how String comparisons actually work.

If you want to see a table of all the values of UTF-16 check this out!

P.S. You marked my answer as negative, don't know if that was intentional or not.

no way.. i marked it as best and negative ..haha.. oops. I was not sure what that button meant. Oh ok so I do not need to worry about this as a devops? or is the number conversion etc going to be important to me?

Dane Parchment
Dane Parchment
Treehouse Moderator 11,075 Points

No the conversion itself isn't going to be important for you, what is important is having a general understanding of what that number is.

So if you know that your language is using UTF-16 for it's character encoding, then you should know that

numbers < Upper Case < Lower Case

when doing String comparisons.

So knowing how they are converted isn't an issue for you. Instead you should again have a general understanding of what they are converted to. A quick way to figure this out, is literally printing the char version of your String, In JavaScript we can view the char code of a character using: charCodeAt().

Other languages will have a similar method.