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.
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!
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';
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:
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’
}
};
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’;
}
}
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.
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';