Is it better to make my conditionals more simplified?

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) {
return ‘adult’;
} 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?

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.

4 Likes

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!

2 Likes

Right on! You’re welcome!

Not to beleaguer the point, but to draw it out…

if (age < 3) { return 'baby' }
else if (age < 13) { return 'child' }
else if (age < 20) { return 'teen' }
else if (age < 65) { return 'adult' }
else if (age < 75) { return 'senior' }
else  { return 'elderly' }

My position would be to favor the expression over the control flow construct. It is readable, and it is maintainable. The language gives us the abstraction to use, not to shy away from.

  return age < 3  ? 'baby' :
         age < 13 ? 'child' :
         age < 20 ? 'teen' :
         age < 65 ? 'adult' :
         age < 75 ? 'senior' : 'elderly';

says it all.

4 Likes

Thanks for this reply. I was just going back over my work from last night looking at how I could refactor the code. I really struggled to see what more I could take out.

Your solution is really simple, I just didn’t see it - your use of:

age < 20

instead of my:

(age >= 13 && age <= 19)

is really elegant.

1 Like