FAQ: Code Challenges: JavaScript Fundamentals - lifePhase()


#1

This community-built FAQ covers the “lifePhase()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise lifePhase()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I need some help, i’ve been trying a few things but doesn’t work, here is my code:

const lifePhase = (number) => {
  if (number <= 3) {
    return 'baby';
  }  else if (number <= 12) {
    return 'child';
  }  else if (number <= 19) {
    return 'teen';
  } else if (number <= 64) {
    return 'adult';
  } else if (number <= 140) {
    return 'senior citizen';
  } else if (number < 0 || number > 140) {
    return 'This is not a valid age';
  }
}

When i log -1 it prints baby.


#3

This suggests making the last condition, the first.


#4

I mean, the last Return is not working.


#5

So move it to the top. Test that condition first, then test valid ages afterward.


#6

Thank you very much.


#7

Regarding this Exercise,please check the following code :
const lifePhase = (age) => {
if (age >= 0 && age < 4) {
return ‘baby’}
else if ( age > 3 && age < 13)
{return ‘child’}
else if (age > 12 && age < 20)
{return ‘teen’}
else if (age > 19 && age < 65) {return ‘adult’}
else if ( age > 64 && age < 141){return ‘senior citizen’}
else if (age < 0 || age > 140){ return ‘This is not a valid Age’}
}

The code above is also logically correct but a rather roundabout way to deal with this exercise; as a result,the system doesn’t seem to accept the code and replies with something like ‘return numbers above 140 or below 0 as ‘This is not a valid age’ .’ as an error.
Please note that I’ve already tried all the possible values and printed them on the console with no problems. Thanks,for taking the time to read this,if you did.


#8

Hey I’m getting the same issue, here’s my code:

const lifePhase = (age) => {
if(age >= 0 && age <= 3){
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 ‘senior citizen’;
}else if( age < 0 || age > 140){
return ‘This is not a valid age.’;
}
}


#9

Don’t know if it’s the cause since i’m also still learning but you have 2 times ; after the return ‘baby’


#10

I’ve tried to solve this problem using a Switch statement. For those of us who attempted to solve this using switch, this is why its the incorrect approach:
'Yes, this is a very common mistake. You are confusing the usage of the switch statement with the if/else statement.

Please keep in mind that you should only use switch as a conditional for specific values. You cannot test conditions such as (1<3) or anything like that. You cannot use comparative operators in a switch statement, that’s what the if/else statements are for."


#11

It may be non-standard usage, but in truth the expressions can be swapped.

switch (true) {
  case age < 0: age = 141;
  case age < 3: return 'baby';
  case age < 13: return 'child';
  case age < 20: return 'teen';
  case age < 65: return 'adult';
  case age < 75: return 'senior';
  case age < 141: return 'elderly';
  default: return "This is not a valid age."
}

Maybe it is not recommended, but that does not mean we cannot do it.


#12

Something else that someone might say, “We cannot do…”

function life_cycle(age) {
  if (age < 0 || age > 140) {
    return "This is not a valid age.";
  }
  return age < 3  ? 'baby' :
         age < 13 ? 'child' :
         age < 20 ? 'teen' :
         age < 65 ? 'adult' :
         age < 75 ? 'senior' : 'elderly';
}
undefined
life_cycle(75)
"elderly"
life_cycle(64)
"adult"

Be that as it may, if we use the language according to syntax rules, we’re half the way to finding interesting and sometimes purposeful ways to do things.