Where is my typo?

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.

Yes I am having the same exact problem!

const lifePhase = (age) => {

if (3 >= age) {
return ‘baby’;
} else if (age >= 4 && age < 13) {
return ‘child’;
} else if (age >= 13 && age < 20) {
return ‘teen’;
} else if (age >= 20 && age < 65) {
return ‘adult’;
} else if (age >= 65 && age < 140) {
return ‘senior citizen’;
} else (age > 140 || age < 0)
return ‘This is not a valid age’;
};

all of my if/ else statements work except for the very last… I don’t see any typos which would make codecademy produce an error, yet it’s still telling me there’s a problem.

else clause does not take a condition. Change (age > 140 || age < 0) to { and things should work as expected.

2 Likes

I was going to reply with my result but that would give others a cheat. I saw the silliness of what I was doing. So any else if statement can take a condition, which means I put the condition in the wrong area. Just confusing that they ask you to return the error at the last part. Thank you so much for your help!

1 Like

else if is the optional else of the previous if-statement, and a new if-statement
the optional else part of an if-statement includes a single statement to execute for which you are providing an if-statement. this is similar to how a for-loop executes the following statement which is often a block statement {}

1 Like

Correct.

if condition A

else if condition B

else if condition ...

else default

Note the role that else plays in the control flow. It’s the fallback of the previous condition when it fails. else is itself conditional since it depends upon this relationship with if.

Control flow essentially falls through the logic structure on every false yield of the successive conditions. The default branch is the last exit from the structure. else is the logic model that leads to this.

1 Like