Codecademy Forums

lifePhase in Javascript course

Hello everyone, this is my first topic post. My question is in regards to the lifePhase exercise in the javscript course. I worked on it for a good while before I finally gave in to see what the answer was, then I still couldn’t figure out why my version wouldn’t have worked. I used the switch method, and the answer used if else.
The answer provided was:

const lifePhase = age => {
if (age < 0 || age > 140) {
return ‘This is not a valid age’
} else if (age < 4) {
return ‘baby’
} else if (age < 13) {
return ‘child’
} else if (age < 20) {
return ‘teen’
} else if (age < 65) {
return ‘adult’
} else {
return ‘senior citizen’
}
}
console.log(lifePhase(15))

And what I had worked on before checking the answer was:

const lifePhase = (age) =>{
switch (age){
case age >= 0:
case age <= 3:
return ‘baby’;
break;
case age >= 4:
case age <=12:
return ‘child’;
break;
case age >= 13:
case age <= 19:
return ‘teen’;
break;
case age >= 20:
case age <=64:
return ‘adult’;
break;
case age >= 65:
case age <= 140:
return ‘senior citizen’;
break;
case age < 0:
case age >140:
return ‘This is not a valid age’;
break;

}

}
console.log(lifePhase (15));

https://bit.ly/2k8Nu5m

Hello, @wickdw8yz. You can read all about how to use switch here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch, but none of your case's as written will be selected. There are ways to use a range in your case's, but the way you attempted isn’t one of them. For extra study, you can look at the following example, and research why it works. Happy coding!

Example using switch with range of values
const lifePhase = (age) =>{
  
  switch (true){
    
    case (age < 0 || age > 140):
      return 'This is not a valid age';
    
    case (age <= 3):
      return 'baby';
    
    case (age <= 12):
      return 'child';

    case (age <= 19):
      return 'teen';

    case (age <=64):
      return 'adult';

    case (age <= 140):
      return 'senior citizen';

  }
}

console.log(lifePhase (25)); //outputs adult
1 Like

Thank you for your feedback. I really do appreciate it. I did my dual cases the way I did from a stack overflow website I seen utilizing it. https://bit.ly/2mc0Bnf. If that is something outdated please let me know. I try to use Codecademy or W3schools.com to find my reference material. From now on I will consider the developer.mozilla.org for my reference material, which I also see it sponsored by Github.

1 Like

I think if you’ll re-read the link to stack overflow, you’ll see a few posts down that the solution to the OP’s question involved a tact similar to the example I shared. The expression inside the parenthesis of the switch() statement has to match a case in order for the code included in the case’s code block to be executed. In the code you tried, the expressions in your case statements are evaluated. In each case the result is either true or false depending on the value assigned to age. Since age is assigned an integer like 15 there cannot be a matching case.

const life = age => { //the value passed to age is 2
  switch(age){  //find the case that evaluates to 2
    case (age <= 3): //2 <= 3 evaluates to true, but true doesn't match 2
      return 'child';
    default: //default will be executed if no match is found
      return 'other'; //other is returned
  }
}

console.log(life(2)); // output: other

If we run this same snippet, but change the expression in the switch() to true:

const life = age => { //the value passed to age is again 2
  switch(true){ //find the case that evaluates to true
    case (age <= 3): //2 <= 3 evaluates to true, so we have a match
      return 'child'; //child is returned
    default:
      return 'other';
  }
}

console.log(life(2)); //output: child

It’s also worth noting that in my examples I omitted the break; statements. Since we are using return to send a value back to the line of code that called the function, there is no need for the break. return effectively terminates the function by sending control (and a value in this case) back to the caller, so the break statement would never be reached even if it was there. If we needed to do something else inside the function before it terminated, we could assign the value 'child' to a variable, use break; to exit the switch(), and then the remaining code in the function could execute. Sorry for the long response. Hopefully it helps. Again, happy coding!

2 Likes