Rock Paper Scissors project, why 4 separate if statements?

In the Rock Paper Scissors project, during steps 7 - 10 when we are determining the winner. Is there a specific reason or even pros/cons to writing it out as 4 separate if statements instead of a single if/else if/else if/else if? It just feels like the other steps are trying to keep the code concise, but then those steps intentionally add extra lines.

Hello @deadpark121, welcome to the forums! Could you please post your code (making sure to format it correctly), and a link to the exercise?

In the Rock Paper Scissors javascript project. I’m curious why you’d make separate if statements, like the code below, instead of else if statements. In a round about way of asking, i guess i kind of want to know what the point of “else if” is if you can just write separate if statements instead.

const determineWinner = (getUserChoice, getComputerChoice) => {
  if (getUserChoice === "nuke"){
    return "You cheated! But since everyone is dead, i guess you win."
  } 
  
  if (getUserChoice === getComputerChoice){
    return "Looks like a tie."
  }
  
  if (getUserChoice === "rock"){
    if (getComputerChoice === "paper"){
      return "Paper covers Rock. Computer Wins."
    }else{
      return "Rock breaks Scissors. Player Wins!"
    }
  }
  
  if (getUserChoice === "paper"){
    if (getComputerChoice === "scissors"){
      return "Scissors cut Paper. Computer Wins."
    }else{
      return "Paper covers Rock. Player Wins!"
    }
  }
  
  if (getUserChoice === "scissors"){
    if (getComputerChoice === "rock"){
      return "Rock breaks Scissors. Computer Wins."
    }else{
      return "Scissors cut Paper. Player Wins!"
    }
  }
}

I suppose you could use else if blocks instead of a chain of if blocks, but it doesn’t really matter.


Now, the difference between if…else if and if…if is that the else if will only run if the if block’s condition is false:

let someVar = "hello world";
if (someVar === "hello world"){
  console.log("hi");
} else if (someVar){ 
  console.log("hello");
}

In the above scenario, the only output to the console is "hi". Compared to:

let someVar = "hello world";
if (someVar === "hello world"){
  console.log("hi");
}

if(someVar) {
  console.log("hello");
}

In that snippet, both hi and hello are logged to the console.