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.

57 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!

4 Likes

Right on! You’re welcome!

2 Likes

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.

27 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.

4 Likes

I had the same issue. Thanks for bringing this up!

This might work, but then it won’t print to the console if the number is an invalid age

It will if there is validation before the return statement.

Thanks mtf, for your contributing,.
I did it this way…; though it longer than yours but i understand my code

// Write your function here:
let lifePhase = age => {
if (age < 0 || age > 140)
return ‘This is not a valid age’
else if (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 ‘This is not a valid age’
}
};

console.log(lifePhase(-1))
console.log(lifePhase(3))
console.log(lifePhase(10))
console.log(lifePhase(0))
console.log(lifePhase(64))
console.log(lifePhase(98))

Hey mtf, thanks for replying, but I made this code, and it keeps coming up with the same error, “If the argument passed in is less than 0, the function should return ‘This is not a valid age’” Could you tell me why this keeps happening? When I call the function with -1 as the age, the console prints undefined.

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

console.log(lifePhase());

-3 is less than 3. That should give you a hint.

Are you saying that I should call the function with -3 as the age?

No, but what I am saying is that if any negative number can get by the first conditional, then the final conditional is never seen. In other words, test for invalid ages before, not after the other valid age ranges.

6 Likes

Ohhh, ok I’ll try that!

Thanks! I tried it out and it worked!

2 Likes

Can anyone tell me why the console prints undefined when calling this function?

// Write your function here:

const lifePhase = age => {

age < 1 ? ‘This is not a valid age.’:

age < 4 ? 'baby':

age < 13 ? 'child':

age < 20 ? 'teen':

age < 65 ? 'adult':

age < 141 ? 'senior citizen':'This is not a valid age.'

}

// Uncomment the line below when you’re ready to try out your function

console.log(lifePhase(0))

console.log(lifePhase(5))//should print ‘child’

console.log(lifePhase(16))

console.log(lifePhase(35))

console.log(lifePhase(90))

console.log(lifePhase(150))

// We encourage you to add more function calls of your own to test your code!

The cause is actually quite simple… There is no return value.

Since the entire body of the function is a single expression, it can be returned.

const lifePhase = age => {
    return age < 1 ? ‘This is not a valid age.’:
    // ... (the rest of the expression)
}

Consider also that less than or equal to zero is not a valid age, but anything greater than that (within range) is…

return age <= 0 ? ‘This is not a valid age.’:
2 Likes

Oh man, thanks for the reply. Wow, so simple is right. I was over complicating it in my head.

1 Like

I only started to learn JS last week and although my code passed I wonder if it’s the right approach:

const lifePhase = age =>
  age < 0 || age > 140 ? 'This is not a valid age'
: age < 4 ? 'baby'
: age < 13 ? 'child'
: age < 20 ? 'teen'
: age < 65 ? 'adult'
: 'senior citizen';