# 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:

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

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

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) {
} 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.

1 Like
#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){
}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."

2 Likes
#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.

3 Likes
#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)
``````

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.

1 Like
#13

what Tim said and you last line (I think) needs to just be “else”… this works:

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

1 Like
#14

can’t find mistake…thought it shout be ok, but I get ‘’ SyntaxError: Unexpected token <=’’.

#15

…yes this way makes more sense…

#16

Hi there, so I wrote my code. Everything checked out and the code worked. When I entered -1, I was returned ‘This is not a valid age.’ When I entered 5, I was returned ‘child.’ 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) {
} else if (age >= 65 && age <= 140) {
return ‘senior citizen’;
} else {
return ‘That is not a valid age.’;
}
};

console.log(lifePhase(141));

I finally got to the point where I asked for the answer, and I saw that the code had the ‘This is not a valid age’ was listed first, and it did not use the <= or && operators. Why does that make a difference in the code? Is that just because it wasn’t as simplified?

#17

Quite likely. Your code checks for between-ness, where the other example might follow a path from lowest to highest and return the value that corresponds with that range.

Either approach is okay, so long as there are no gaps, but the one with less logic and verbosity is always going to win out in the end. It just makes sense to want simplicity over complexity.

You should feel good that your code works and take comfort in the fact that everybody’s code can do with a little look see to find ways to refactor and simplify. It never means throwing the baby out with the bath water. Ideas are not always simple to express so we will generally write naive code to give expression to our ideas. Keep writing that way to start any project.

Expect to simplify, but do it later. Keep the juices flowing in the most general ways you can. Get the ideas out there and test as you go. It should be up and running before any refactoring takes place. That way we have a road map of the abstraction process.

1 Like
#18

Thanks for the advice and encouragement! While I understand JavaScript and functions, I often get stuck when there’s that one little thing that’s stopping the code from running on Codecademy’s program. It’s a relief to know that I’m on the right path!

1 Like
#19

Right on! You’re welcome!

#22

but like honeslty, THIS SHOULD TOTALLY WORK
AAAHHHHH breaks computer