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

Question

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

Answer

We can use multiple if statements when we want to check multiple conditions no matter if the previous if statement was true/false. With multiple if statements, each statement will be checked even when a previous statement evaluates to true.

We want to use if statements along with else if statements when we want to check multiple conditions based on the evaluation of the previous statement, the conditions will stop being checked when a statement evaluates to true.

Example:

let num = 40;

//using only `if` statements, each of the following conditions will be checked

//the following condition evaluates to `false`, the next condition will be checked
if(num < 10){
  //do something
}

//the following condition is the only on that evaluates to `true`, code inside this `if` block will execute
if(num >= 10 && num <= 50) {
  console.log('test')
}

//this condition will still be checked even though the previous condition was evaluated to `true`
if(num > 50) {
  //do a thing
}

vs.

let num = 40;

//using `if` and `else if` statements the code will stop checking conditions after one of the conditions is evaluated to `true`

//this first condition is checked, evaluates to `false`, the next condition will be checked
if(num < 40){
  //do something
}

//the previous condition was evaluated to `false` so the following condition will be checked, and it evaluates to `true` so code inside this `else if` block will execute
else if(num >= 10 && num <= 50) {
  //do something else
}

//the following condition will not be checked as the previous condition evaluated to `true`
else if(num > 50) {
  //do a thing
}
4 Likes

In the below method, what is wrong!!??
all the time user only winning the game!! :stuck_out_tongue:

const determineWinner = (gerUserChoice,getComputerChoice) =>{
  //Checking for Tie
  if(getComputerChoice === gerUserChoice ){
  return 'Its a Tie ';
  }
  
  //Checking for the base condition of rock
  else if(gerUserChoice === 'rock'){
    if(getComputerChoice === 'paper'){
      return 'Computer Won!!';
    }
    else{
      return 'User Won!!';
    }
  }
  
  //checking for the paper condition
  
  else if(gerUserChoice === 'paper'){
    if(getComputerChoice === 'scissors' || getComputerChoice === 'rock'){
      return 'Computer Won!!';
    }else{
      return 'You Won!!';
    }
  }
  
  //checking for the scissors condition
  else if(gerUserChoice === 'scissors'){
    if(getComputerChoice === 'rock'){
      return 'Computer Won!!';
    }else{
      return 'You Won!!';
    }
  }
};
2 Likes

I’m not 100% sure, but maybe there’s a problem with the variable placeholders you used. The lesson says to use userChoice and computerChoice in the determineWinner function - while getUserChoice and getComputerChoice call different functions. This is what I had:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'We have a TIE!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer has smothered you.';
    } else {
      return 'You have crushed the computer!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer has cut you.';
    } else {
      return 'You have smothered the computer!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer has crushed you.';
    } else {
      return 'You have cut the computer!';
    }
  }
}

I have used the parameters used in the function to compare!!!.
however, when both user and computer choice is same for ex: paper, still it is showing user winner!!!

I will try again today!! :slight_smile:

Although not ideal, its possible. The parameters have a local scope while the function have a global scope, so they can co-exist, but like i said, not ideal

1 Like

Can we please see your full code? It might shed some light on this mystery.

1 Like

hi there.
In the exercise “lifePhase”, i ran this code :

function 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'
  }
}
console.log(lifePhase(5));

…and the result was wrong in compare to yours.
I went then to Codepen, i ran it and the result was : child .
Why was that??
Why your site told me that i was wrong,since its proved that the code above its correct.
Thanks in advance.

Edge case. What if the age is 12:

console.log(lifePhase(12));

we get: This is not a valid age. How is being a 12 year old not a valid age?

2 Likes

So, in the LifePhase exercise I did this code:

function lifePhase(age) {

if (age <= 3 && age >= 0) {

  return 'baby';

} else if (age <= 12 && age >= 4) {

  return 'child';

} else if (age <= 19 && age >= 13) {

  return 'teen';

} else if (age <= 64 && age >= 20) {

  return 'adult';

} else if (age <= 140 && age >= 65) {

  return 'senior citizen';

} else {

  return 'This is not a valid age.'

}

};

I’m running all the ages to test and they all seems to be working, even the ones under 0 and over 140, but still it says the exercise is not right, giving me the error:

“If the argument passed in is less than 0, the function should return ‘This is not a valid age’”

but, as I said, if I put 150 or -5, it runs and says “This is not a Valid age”

please help me… :sob:

Without the exercise url its difficult to say, i would guess the string returned is not exact match. Maybe there shouldn’t be a full stop at the end of the sentence?

yeah, I totally forget to link the exercise. This is the One!

That only confirms what i already suspected:

You need to specify the condition of below 0 and above 140, you used the else statement, which is oke for the outcome, but not for the assignment.

1 Like

I think there is a problem in the condition above:

If getUserChoice equals ‘paper’

Then you are saying:

if getComputerChoice equals ‘scissors’ the computer will win and
if getComputerChoice equals ‘rock’ the computer also will win?

Paper beats rock

1 Like

I solved it by having the first “if” statement check that it was a valid age .

const lifePhase = (age) => {
if (age < 0 || age > 140) {
return ‘This is not a valid age’;
} else 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 {
return ‘ERROR’;
}
}

2 Likes

Hello everyone, this is what I did for LifePhase, seems to work.

Anything wrong with this that I might have missed? I see that it is a bit different to the samples above.

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

Hi there!
I tried both options and have one question for the first one (where there are only ifs) and is:
I have two if statements that evaluate to true, the code appears to return the result of the first statement that evaluated to true not the latest one, am I right?

1 Like

Hi!

It doesn’t appear you have a reply so I am going to try (I am a newbie myself but maybe this makes the concepts easier for me). It appears that your ages / conditions are all “greater than” except for your “baby” condition.

I’m curious to know what your result is when you log it.

Perhaps your validity range should be written out as an “or” statement?

E.G.

const lifePhase = x => {
if (x < 0 || x > 140) {
return ‘This is not a valid age’;
}

2 Likes

Hi everyone, I have a curious error I am not sure how to reconcile it, it is on the “lifePhase” JS exercise.

Here is my code:

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

I’ve logged several ages and it has correctly categorized my number, however, when I add a letter input, I receive this:

console.log(lifePhase(aaa));

Screen Shot 2020-07-06 at 9.31.14 AM

I thought my last “else” statement would address this or any other strange input that isn’t numerical. Any suggestions?

Letters are strings in JS. Currently, I only see a undefined variable.