# Why 2 if statements and not just an if/else?

So this i’m in the middle of doing the rock paper scissors project and i’m not having any issues but i’m wondering…On step 7 i create an if statement to check whether both parties throw the same hand, but then in step 8 it says to make another if statement to check if i threw rock and the computer threw something else other than rock and return the value with a string, But why not just continue the if statement from step 7 and just do an if/else? Here’s the code and link to the project. Rock, Paper, Scissors Project

``````const getUserChoice = userInput => {
userInput= userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput ==='scissors') {
return userInput;
} else {
return console.log('Error!');
}
}

const getComputerChoice = () => {
const randNum = Math.floor(Math.random() * 3);
switch (randNum) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
return 'Error!'
break;

}
}

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return `Both participants threw \${userChoice}, its a draw!`;
} else if (userChoice)
}
``````

Incase there is confusion i could do…

``````else if (userChoice === 'rock' && computerChoice === 'paper') {
return  'The user threw rock and the computer threw paper...the computer wins!';
} else if ......
``````

instead of 2 if statements followed by a return of the winner.

Never mind, i kind of get it it now. It reduces the amount of code, instead of having to write a scenario for each possibility for me throwing rock, it will check for both remaining possibilities left in 1 statement instead of 2 since throwing the same hand as the computer is already a calculated possibility.

This is the answer, but not for the reason you gave.

The biggest reason we might use this:

``````if (x) {
return 1;
}
if (y) {
return 2;
}
``````

is to remove characters. That said, if you did this:

``````if (x) {
console.log(1);
}
if (y) {
console.log(2);
}
``````

you’d be wasting time.

This is because when you use `return`, you’re ending the function with a return value. If `x` is `true` in the first example, the second `if` statement won’t even run. In the second example, however, both will run, since `console.log()` doesn’t end the function.

In the second case, you’d be better off doing this:

``````if (x) {
console.log(1);
}
else if (y) {
console.log(2);
}
else {
// ...
}
``````

or

``````if (x) {
console.log(1);
}
else {
if (y) {
console.log(2);
}
}
``````

In the last two examples, the second `console.log()` won’t run if `x` is true.

Hmmm, okay well to start off, my question is why 2 if statments and not just an if else? like

``````if (blank) {
if (blank2) {
return 'Hello'
} else {
return 'Goodbye'
}
}

``````

could be written as

``````if (blank, blank2) {
return 'Hello'
} else if (!blank, blank2) {
return 'Goodbye'
} else if....
``````

if im not mistaken i think that would work i would just have to console.log the function that the return value is stored in or add console.log into the return part.

I will admit tho it is good to know that console.log would cause a program to continue running if i didnt use return, im sure id have done it on accident at some point so thanks!

Hi,
You can usually write if, if else, etc conditions in a number of different ways.
Which you choose might depend on how you want to set the code out, or to improve readability.
For example, say if you had a function which took in a int. You didn’t want the code to run if it was 0.
You could either have;

`````` if (n!=0){
{rest of code block}}
}

OR

if (n==0){
return;
}
{rest of code block}
}

Both do the same but, to me, the second is clearer. The edge case is dealt with and can no longer be an issue inside the function.
Similarly with if / else if. You may find one way is clearer to you or one makes more sense with the way the code flows.``````