Codecademy Forums

Switch keyword and function on CODE CHALLENGES: JAVASCRIPT FUNDAMENTALS (4/15)

I don’t get why my function keeps returning undefined when I right like this, but works when I use if…if else statements! Am I using the switch keyword wrongly? Thanks a lot!

function lifePhase(age) {
  switch (age) {
    case age >= 0 && age<= 3:
      return 'baby'
      break;
    case age >= 4 && age <= 12:
      return 'child'
      break;
    case age >=13 && age <= 19:
      return 'teen'
      break;
    case age >= 20 && age <= 64:
      return 'adult'
      break;
    case age >= 65 && age <= 140:
      return 'senior citizen'
      break;
    case age <= 0 || age >= 140:
      return 'this is not a valid age'
      break;
  }
}
console.log(lifePhase(5))
2 Likes

Switch statements usually use one check:

function lifePhase(age) {
    switch (age) {
        case age 1:
        break;
        // etc...
    }
}

I did some research and found that, to accomplish what you want to, you can do the following:

function lifePhase(age) {
    switch (true) {
        case age >= 0 && age<= 3:
        return 'baby';
        case age >= 4 && age <= 12:
        return 'child';
        case age >=13 && age <= 19:
        return 'teen';
        case age >= 20 && age <= 64:
        return 'adult';
        case age >= 65 && age <= 140:
        return 'senior citizen';
        default:
        return 'this is not a valid age';
    }
}
console.log(lifePhase(5))

Setting the switch variable to true seems to work.

2 Likes

Interesting… Thank you a lot!!

Given the top down flow of the switch, we can use the same type of order that we would for if..else if..else. and avoid the verbosity of logical expressions.

case age < 0: age = 141;    // set to fail all cases and reach the default
case age < 4: return 'baby';
case age < 13: return 'child';
case age < 20: return 'teen';
case age < 65: return 'adult';
case age < 141: return 'senior';
default: return 'This is not a valid age;

The key is that the value in the case must match or satisfy the value/expression in the switch argument. boolean => boolean; number => number; string => string. Above we use the boolean expression match.

5 Likes

im wondering, why would you put true, and why would it help?

Why match on true?

A switch compares one value to others. In this case they use booleans as those others.


If-statements already match on true, so switching on true is rather weird.

I would MUCH rather see:

function lifePhase(age) {
  if (age >= 0 && age <= 3) {
    return 'baby'
  }
  if (age >= 4 && age <= 12) {
    return 'child'
  }
  if (age >= 13 && age <= 19) {
    return 'teen'
  }
  if (age >= 20 && age <= 64) {
    return 'adult'
  }
  if (age >= 65 && age <= 140) {
    return 'senior citizen'
  }
  return 'this is not a valid age'
}

(Seriously, if there’s a switch I’m gonna start thinking it’s NOT something if-statements are good for and generally have a very hard time reading it)

Still repeating code, so, better yet if you ask me is:

function lifePhase(age) {
  let classifications = [
    [0, 3, 'baby'],
    [4, 12, 'child'],
    [13, 19, 'teen'],
    [20, 64, 'adult'],
    [65, 140, 'senior citizen'],
  ]
  for (let [low, high, classification] of classifications) {
    if (low <= age && age <= high) {
      return classification
    }
  }
  return 'this is not a valid age'
}
1 Like

very interesting! you introduced me to a new concept! but what does this do?


  for (let [low, high, classification] of classifications) {
    if (low <= age && age <= high) {
      return classification
    }
  

(sorry if its a lame question, im new)

Not sure what you’re asking about.
But I would guess it’s either for-of, or destructuring. Both of which you can google. (pick results from mozilla developer network)

You should also be able to make pretty good guesses from knowing what the function overall does and what the rest in the function is.

1 Like

const lifePhase = (age) => {
if (age < 4 && age > 0 )
{
return “baby”
}
else if ( age >= 4 && age <= 12 )
{
return “child”
}
else if ( age >= 13 && age <= 19 )
{
return “teen”
}
else if ( age >= 20 && age <= 64 )
{
return “adult”
}
else if ( age >= 65 && age <= 140 )
{
return “adult”
}
else
{
return ‘This is not a valid age’
}
}

console.log(lifePhase(13))

IT IS WORKING BUT IT IS NOT ACCEPTED …

Hello, @bit8334075178. Welcome to the forum!
Looks like you are returning “adult” twice instead of “senior citizen” for those in the last category.

Also, for future posts, when you want to paste in your code, please click the </> button first, and then past your code in the space indicated. It will preserve your formatting, and make your code appear as it does in this post. Happy coding!

1 Like