In an attempt to achieve the most concise solution to the lifePhase() function exercise, I minimized the number of conditional statements, avoided extraneous comparison/logical operators, avoided else after return and avoided unnecessary {}-delineated blocks. Here’s my (tested) solution:
function lifePhase(age) {
if (age > 140) return 'This is not a valid age';
if (age > 64) return 'senior citizen';
if (age > 19) return 'adult';
if (age > 12) return 'teen';
if (age > 3) return 'child';
if (age >= 0) return 'baby';
return 'This is not a valid age';
}
Why do they want you to do the exercise exactly as they want?!? There are more ways to write this exercise and if the way we made provides the same correct answer, then why do it their way??? Its frustrating and makes me feel like I coded something wrong!
lifePhase = (num) => {
if (num >= 0 && num <= 3) {
return “baby”;
} else if (num >= 4 && num <= 12) {
return “child”;
} else if (num >= 13 && num <= 19) {
return “teen”;
} else if (num >= 20 && num <= 64) {
return “adult”;
} else if (num >= 65 && num <= 140) {
return “senior citizen”;
} else {
return “This is not a valid age”;
}
};
console.log(lifePhase(-1));
The last ELSE should be only if that is off/no other situation/other change, === learn code less, mean dont write big code, if you can make that shorder.
For example:
if (age>140)//do something
if(age<0)// do something
That is not so much good, if you can coding less, than make that in last chance like if or if else not working, then otherwhise { else } //do something
And the girl/ladie answer and ask,
If you use only if statement everywhere, its not so much good, because the scope code what you was writing, will be checked even you statement was successfull accepting.
So better is if you write if and else if in the scope, not only if if if, because you code will checking everything in scope, that mean more work for you web, or for you PC.
Hi! I like to use ternary operator instead of If - else or switch operators.
here’s my first solution for lifePhase( ) exercise:
const lifePhase=age=>{
return (0 <= age && age <= 3) ? ‘baby’
: (4 <= age && age <= 12) ? ‘child’
: (13 <= age && age <= 19) ? ‘teen’
: (20 <= age && age <= 64) ? ‘adult’
: (65 <= age && age <= 140) ? ‘senior citizen’
: ‘This is not a valid age’;
}
As each code line will evaluated from top to bottom, it could be solved with less code and look like this:
Expressions like 0 < age <= 3 make sense mathematically. This expression checks whether age is in the interval (0, 3]
But programming languages differ in how they handle chained comparisons such as these.
In Python, the above will work and will be interpreted as (0 < age) and (age <= 3).
In JavaScript, it is evaluated differently.
age = 5
// < and <= have equal precedence, so
// evaluation is done left to right
0 < age <= 3
// 0 < 5 evaluates to true
true <= 3
// Comparing boolean to number, so
// boolean is coerced to number
// true is coerced to 1, false is coerced to 0
1 <= 3
// Evaluates to true
// First if condition evaluates as true and
// "baby" is returned.