Does the order of else if conditions matter?

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) {
    return 'adult';
  } 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.

This suggests making the last condition, the first.

I mean, the last Return is not working.

So move it to the top. Test that condition first, then test valid ages afterward.

Thank you very much.

1 Like