0

I have just started with Javascript and trying to learn, just the basic stuff at the moment. But the thing is I honestly dont get how this switch statement returns false if age = 0

function isUnderAge(age){
    const MAXNUMBER = 120;
    const MAXAGE = 18;
    const MINAGE = 0;
    switch(age){
        case(age < MAXAGE && age >= MINAGE && age <= MAXNUMBER):
            return true;

        default:
            return false;
    }
}

console.log(isUnderAge(0));

Does anyone else see the issue?

Any help would be highly appreciated!

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
aitsimhand
  • 13
  • 5
  • `switch` compares the argument to the cases, in this case, `age` to a boolean. I am sure there is a good dupe for this, but admit it's probably hard to find. – ASDFGerte Apr 13 '21 at 15:13
  • Do you think `age` is equal to `age < MAXAGE && age >= MINAGE && age <= MAXNUMBER`? Because only then would your code match the case. However, that's most likely not possible as `age` would be a number while the next expression will be a boolean and so, `age === age < MAXAGE && age >= MINAGE && age <= MAXNUMBER` would always be `false`. If you need a *complex condition*, use an `if` statement. If you need just literal comparison `age === 1` or `age === 2` etc, you can use a `switch(age) { case 1: /* ... */ case 2: /* ... */ }`. – VLAZ Apr 13 '21 at 15:26

4 Answers4

1

You are comparing age with boolean because age < MAXAGE && age >= MINAGE && age <= MAXNUMBER leads to boolean value so it always return false

In your case age is 0 which is compared with age < MAXAGE && age >= MINAGE && age <= MAXNUMBER which leads to true. So comaring 0 === true which doesn't match so it will go to default case

function isUnderAge(age) {
  const MAXNUMBER = 120;
  const MAXAGE = 18;
  const MINAGE = 0;

  if (age < MAXAGE && age >= MINAGE && age <= MAXNUMBER) {
    return true;
  } else {
    return false;
  }
}

isUnderAge(12);
isUnderAge(40);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • `return age < MAXAGE && age >= MINAGE && age <= MAXNUMBER` – VLAZ Apr 13 '21 at 15:43
  • @VLAZ Just see OP's question and think OP don't know how to write a switch statement, then OP must be starting his career in programming. It would be hard to understand for OP. If/else is very basic so I've added to help OP. ☺ – DecPK Apr 13 '21 at 15:46
0

This isn't really the use case for a switch. Have a look below:

function isUnderAge(age){
    const MAXNUMBER = 120;
    const MAXAGE = 18;
    const MINAGE = 0;
    
    if(age < MAXAGE && age >= MINAGE && age <= MAXNUMBER)
    {
        return true;
    }
    return false;
}

console.log(isUnderAge(0), isUnderAge(17), isUnderAge(20), isUnderAge(130));
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
  • Incidentally, that's not really the job of an `if` when all it does is return the result of the condition. `return age < MAXAGE && age >= MINAGE && age <= MAXNUMBER` is much cleaner than returning `true` only when this expression is `true` and returning `false` when the expression is `false`. – VLAZ Apr 13 '21 at 15:43
0

A little cleaner alternative

function isUnderAge(age) {      
  const MAXNUMBER = 120;
  const MAXAGE = 18;
  const MINAGE = 0;

  if(age < MINAGE) return true;
  return (age < MAXAGE) && (age <= MAXNUMBER) ? true : false;
}

console.log(isUnderAge(18));
console.log(isUnderAge(-1));
console.log(isUnderAge(17));
  • `if(age < MINAGE) return true;` so...if I'm 5 years old, I can buy alcohol because I'm less than 18 years old? – VLAZ Apr 13 '21 at 15:41
  • @VLAZ the question is: isUnderAge?. If you have 5 yars old the answer is YES, or TRUE in the boolean case. Read the function name. So you are under age to buy Alcohol. You can invert the logic, but you have to refatoring the code and change the function name, for sure: const hasLicenseToBuyAlcoohol =(age)=> age >=18; – Fabio Ribeiro de Carvalho Apr 13 '21 at 16:56
0

as the comments say you are getting a boolean out in each case . just like you are saying case true return true case false return false in which switch will fallback to the default case also don't forget break if you use switch

Nashaat Mohamed
  • 341
  • 2
  • 6