FAQ: Code Challenges: JavaScript Fundamentals - lifePhase()

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!

5 posts were split to a new topic: Does the order of else if conditions matter?

A post was split to a new topic: Where is my typo?

3 posts were split to a new topic: Why doesn’t this conditional work?

8 posts were split to a new topic: How could I use a switch statement for this lesson?

3 posts were split to a new topic: What is wrong with my conditionals?

5 posts were split to a new topic: Is it better to make my conditionals more simplified?

2 posts were split to a new topic: Why does this code not work?

Why does this not work?
function lifePhase(age) {
if (age < 0 || age > 140) return ‘This is not a valid age’;
if (age < 4) return ‘Baby’;
if (age < 13) return ‘Child’;
if (age < 20) return ‘teen’;
if (age < 65) return ‘adult’;
if (age < 141) return ‘senior citizen’;
}

I get the right results for every category, but CodeAcademy keeps telling me “If the argument passed in is greater than or equal to 0 and less than or equal to 3, the function should return ‘baby’”

strings returned need to be an exact match, this is case sensitive.

lifePhase() Challenge

If the argument passed in is less than 0, the function should return ‘This is not a valid age’

Hey there! I have (-2) as a test and get the above message. I believe it should be correct but here we are. If someone is available to review, I would appreciate it.
:grin:

strings needs to be an exact match (recommend copy pasting), you have a typo in valid

I hope I’m posting this in the correct place…

Can someone tell me what is wrong with this code:

function lifePhase(age) {
if (0 <= age <= 3){
return ‘baby’
} else if (3 < age <= 12){
return ‘child’;
} else if (13 < age <= 19){
return ‘teen’;
} else if (20 < age <= 64){
return ‘adult’;
} else if (65 < age <= 140){
return ‘senior citizen’;
} else if (age < 0 || age > 140){
return ‘This is not a valid age’;
}
}
console.log(lifePhase(35));

1 Like

This manner of inequality is not recognized in ES.

if (0 <= age && age <= 3)
2 Likes

I thought that was it. Thanks.

1 Like

Hi i hope am also posting this in the correct place.
Is there a possibility of using switch instead of if/else if statements? Here is the code I wrote using switch:
const lifePhase = age =>{
switch(age){
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’
}
}
It just goes right to default return, i replaced it later with if/else if statements and it worked. Can some one explain me why it didn’t work with switch?

1 Like

What is the one thing that all the cases resolve to? A boolean. That means the argument expression should be a boolean or they will never match.

switch (true) {
4 Likes

Hello! First time posting in the forums so I hope I make it into the correct place.

So my code I used for the lifePhase() challenge code was

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 <=65){
  return 'adult';
} 
else if(age >=65 && age <=140){
  return 'senior citizen';
} 
else {
  return 'This is not a valid age';
}
};

and the code that was given from the solution 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’
}
}

I just want clarification on why my code worked, but did not get passed. I think maybe because the invalid option was set last instead of first when I compared the different codes.
Thank you!

1 Like

no, that doesn’t matter. When i run your code, i get the following error message:

If the argument passed in is greater than or equal to 65, the function should return ‘senior citizen’

if we put that to the test:

lifePhase(65)

we get adult, while we should get senior citizen.

I see very good ideas in this topic to implement in this challenge. But someone could help me with my original code:

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

it returns : ‘SyntaxError: Illegal break statement’.