FAQ: Code Challenges: JavaScript Fundamentals - lifePhase()

God, can any one please help me how to pass this lifePhase() task, my code is working fine but it is not letting me to move forward by throwing this error,
“If the argument passed in is less than 0, the function should return ‘This is not a valid age’”.
can anyone please help me by giving your code just to through this task and please check out my code

function lifePhase(age){

         if(age < 0 || age > 140){
           
           return "This is not a Valid age"
         }else if(age === 0 || age < 4){
           return "baby"
         }else if(age===4 || age < 13){
           return "child"
         }else if(age===14 || age < 20){
           return "teen"
         }else if(age===20 || age < 65){
           return "adult"
         }else if(age===65 || age < 141){
           return "senior cirizen"
         }
} 

  console.log(lifePhase(-39));

i did copy paste the strings from the task, but still it didnt worked

@stetim94 i did copy paste, but it still not working.

Can see an updated version of your code then? In the code you posted Valid is still with uppercase V while this should be lowercase

@stetim94 thank you so much, aghh i wasted so much time, why they validating in case sensitive mode.

string comparison is case sensitive by default

what I don’t understand is, I already covered this in my first answer:

why did that go unnoticed? Just my curiosity to understand how I can improve my answers.

Compare that to the error message.

Hey,
Seems that i have a problem. I always getting a the same error 'If the argument passed in is less than 0, the function should return ‘This is not a valid age’

Any ideas?

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';
  } else {
    return 'This is not a valid age';
  }
}

you could simple add a call to see what you get returned:

console.log(lifePhase(-5))

which will log baby. Which make sense. -5 is smaller then 3, so the if condition is true

Thanks! I get that now!

Can anybody lend insight?

function 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';

}

}

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

Without peering too deeply, consider,

  1. return is used in every clause so we don’t need else.
  2. Validating first, before continuing is a way of getting invalid inputs out the way.
  3. If we go from smallest to largest we don’t have to test a lower bound.
  4. Ditto we don’t need added logic.
if (%invalid%) return ‘This is not a valid age’
if (age < 4) return 'baby'
...
if (age < 65) return 'adult'
return 'senior citizen

ahh okay! I think i solved my issue. I read a comment just above mine that said if the variable is <= 3 or >= 0, a negative number constitutes <= 3 which will give me an error. I fixed it by adding && instead of the initial ||.

1 Like

Consider also what happens if age is a float. How will your logic deal with that?

I understand floats to be decimal numbers. I’ve reworked my code to match the example you gave and it seems more straight forward than what I had prior. The code ran perfectly, I tried floats and they worked also.

1 Like

Would anyone be able to shine a light on why this piece of code isnt working?

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

else can’t have a condition, given else is everything else.

I would also change the order, I would check if the age is valid first

1 Like

done! thank you kind sir!