When should I use multiple if statements? When should I use if statements with else if statements?

Finally got this to work. I had to put age in front of both of the comparison arguments.

let 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 'This is not a valid age';
  }
};

Hope this helps!

1 Like

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';
}
2 Likes

Thanks a lot. I’ve had the same issue. It did work. :cowboy_hat_face:

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! :frowning:

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.

Hope make sence :slight_smile:

you should put another if statement
if(age < 0 || age > 140){

return 'This is not a valid age';

}

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:

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

Hi ,

I m trying to solve exercises which is below but there is one problem.

Problem;
When i change the age from console.log(lifePhase(10)); , output is always

‘baby’

Anyone else to help me??

const lifePhase = (age) => {
  if (0 < age <= 3) {
    return "baby";
  } else if (4 <= age <= 12) {
    return "child";
  } else if (13 <= age <= 19) {
    return "teen";
  } else if (20 <= age <= 64) {
    return "adult";
  } else if (65 <= age <= 140) {
    return "senior citizen";
  } else {
    return "This is not a valid age";
  }
};

console.log(lifePhase(5));

Blockquote

1 Like

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.

Documentation: JavaScript Operator Precedence Table

// Javascript
console.log(0 < 20 < 6); // Evaluates as true

// Python 
print(0 < 20 < 6) // Evaluates as False
1 Like

this is the one I created with a bit simplified and reduced some redundancy on the if condition

const lifePhase = age => {
  if (age < 0 || age > 140) {
    return 'This is not a valid age';
  } else if (age < 4) {
    return 'baby';
  } else if (age < 13) {
    return 'child';
  } else if (age < 20) {
    return 'teen';
  } else if (age < 65) {
    return 'adult';
  } else if (age < 141) {
    return 'senior citizen';
  }
};

console.log(lifePhase(3)) //should print 'child'