lifePhase exercise

Hi everyone!

I tried to do the lifePhase exercise using the switch, however, it didn’t work, and I don’t understand why. Can anyone help me?

https://www.codecademy.com/journeys/back-end-engineer/paths/becj-22-software-engineering-foundations/tracks/becj-22-javascript-syntax-part-i/modules/wdcp-22-practice-javascript-syntax-variables-data-types-conditionals-functions-ca8ce054-4684-4862-85c9-20556c93e3c2/lessons/javascript-fundamentals-code-challenge/exercises/life-phase

const lifePhase = age => {
switch(age) {
case (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;
default:
return “invalid”;
break;
}
};

console.log(lifePhase(5)) //should print ‘child’
I am getting Invalid

In switch(age), the age expression evaluates to a number.

Your cases (age <= 3) etc. evaluate to booleans (true/false).

You could edit your switch expression to avoid the mismatch,

switch(true) {
    case (age <= 3):
        return "baby";
    case (age >= 4 && age <= 12):
        return "child";
    ...

Also, since you are returning in the cases, so the break isn’t necessary. If you match a case, you return immediately so there is no danger of fall-through. If you weren’t returning, then break would be needed to avoid fall-through.

Furthermore, the specifications mention:

If the number is less than 0 or greater than 140, the program should return ‘This is not a valid age’

Try testing your current program with a negative age. Do you get the expected output?

Switching for the boolean worked!! thanks.

As you mentioned, the highlighted specification did not deliver the right message with a negative age. Why?

The cases are going to be evaluated top to bottom. Try walking through your cases from top to bottom with a negative age. If you neither reached the "This is not a valid age" case nor the default case, then you must have matched an earlier case. Think about why that case matched and how you can rectify it.

Would that not be the default case? Just sayin’.

In the original post by christian_dellon, the code is:

const lifePhase = age => {
    switch(true) {
        case (age <= 3):
            return "baby";
        ...
        case (age < 0 || age > 140):
            return "This is not a valid age";
            break;
        default:
            return "invalid";
            break;
}
};

Consider the function calls,

console.log(lifePhase(155))
// "This is not a valid age"

console.log(lifePhase("abc"))
// "invalid"

The specifications for the exercise don’t mention the return of the string "invalid", so "This is not a valid age" should suffice as the default.

But, in the code in the original post, the case (age < 0 || age > 140) and default clauses are distinct.

1 Like

Got it!!! when I reordered the code it worked! Thanks for the help! I will be paying attention to this from now on.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.