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’”
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.
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));
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?
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!