Whats the more than 24 doing

Tell us what’s happening:
Okay I am asking too many questions but its all about understanding these exercises. There is stuff that seems totally random and it’s not being explained.

Your code so far


function testLessOrEqual(val) {
if (val <=12) {  // Change this line
  return "Smaller Than or Equal to 12";
}

if (val <="24") {  // Change this line
  return "Smaller Than or Equal to 24";
}

return "More Than 24";
}

testLessOrEqual(10);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Comparison with the Less Than Or Equal To Operator

Link to the challenge:

Firstly, remove that last return and the closing curly brace.

I don’t understand, I am not on the exercise, what curly brace are you talking about?

Hello there,

This lesson is walking you through two things:

  1. There is such a thing as a <= (less than or equal to) operator. What it does is quite self-explanitory.
  2. Javascript is a slightly unique language in that both of the following statements are equivalent to one another:
if (someNumber <= 12) {...} // I randomly chose these values as examples
// Same as
if (someNumber <= '12') {...} // JS converts the string '12' to the
// number 12 before comparing if it is less than or equal

Hope this helps

Hi @martin21,

I’m not quite sure what your question is, so I’ll run through the exercise clarifying a few things. Let me know if my answer didn’t clarify what you were wanting to clarify.

What this function (testLessOrEqual) is doing, is checking if the value of the variable (var) is less or equal than 12 (in the first if statement), and if it’s less or equal than 24 (in the second if statement).

The way if statements work is by running or skipping the code between the { } if the code between the ( ) is true or false:

if (true) { 
  // this will run
}

if (false) {
  // this will not run
}

So, that means that something like this will run:

if (10 > 5) {
  // this will run
}

And if we add variables to the mix, it will look something like this:

var a = 10;
if (a > 5) {
  // This will run because a is 10... 
  // if that changes in the future (to be 3 for example), 
  // this code may stop running
}

That’s the magic of conditional code (like if statements), it allows you to either run code or skip it if something that you want happens or not.


The <= and >= symbols simply mean:

  • <= less or equal.
  • >= greater or equal

Now, when it comes to the

if (val <= "24") {
  // ...
}

It works the same as if the "24" didn’t have quotation marks around it. JavaScript, in this situation, reads it the same as if it said 24.

The reason for this is something called “type coercion”. It’s something that JavaScript does. It simply means that, when there is a simple comparison operator (like >=, <=, == or !=), JavaScript transforms the "24" into 24. It changes the ‘type’ (string, number, boolean, etc.) from a string (whatever is between " " is a string in JavaScript), to a number.

In other cases, it changes numbers to strings… It tries to figure out what you were trying to do, and help you do it. In the case above, it figured out that you were trying to compare two numbers together, so it turned the string “24” into a number so that the comparison worked. Other programming languages (like Java) won’t let you compare a number with a string. It will just throw an error.

This is a feature of JavaScript that some people find confusing and frustrating. There’s even a whole version of JavaScript called TypeScript, that was developed to remove this from JavaScript.

If you want to learn more about this, you can google JavaScript Type Coercion. There’s even a freeCodeCamp article about type coercion in JS.

I hope this helps.

Let me know if something else is not clear :slight_smile:

Have a good one,

John

Thanks John, I understand the less than and greater than concept. It’s just I had some input to line 2, <=12 and line 6, <=24 but this “less than 24” was just sitting there doing what?
It’s the language or syntax of the exercise I find confusing. I have moved on, maybe I am over thinking it. I just don’t like unexplained details I feel I am missing something but because I am getting the right answer, the question does not get resolved.

this is the starting code:

function testLessOrEqual(val) {
  if (val) {  // Change this line
    return "Smaller Than or Equal to 12";
  }

  if (val) {  // Change this line
    return "Smaller Than or Equal to 24";
  }

  return "More Than 24";
}

testLessOrEqual(10);

it’s asking you to put in there conditions that make the correct return statements execute based on the input number

So full disclosure I deleted my previous comment because I forgot that JavaScript automatically does the conversion and I have been working with this language for years.

While it will do the conversion and that is a good thing to be aware of, I would suggest that in real practice, use the type that you actually want to check for as it is more explicit and strait forward when reading the code.

1 Like

If you’re referring to the part where it says return "Smaller Than or Equal to 24", it’s really not doing anything. It just returns that string, no purpose other than that.

I hear you @martin21, it can be confusing/frustrating when that happens.

When I find something that I can solve, but I can’t fully understand, I just make a note of it to research later. However, in my experience learning web-dev, when I don’t fully understand something, it will keep coming up over and over again until I do. So there’s no pressure to understand it fully on my first exposure :slight_smile: