Adding more if / else statements

If the user chooses rock and the computer chooses scissors, are those conditions covered somehow ? it does not seem like it is.

// Define a 'const' function for determining a winner.
const determineWinner = (userChoice, computerChoice) => {

    if (userChoice === computerChoice) {

        return "This game is a tie";
    
    } 
    if (userChoice === "rock") {

        if (computerChoice === "paper") {

            return "The winner is the computer";
        
        } 
        
        return "The winner is the user";
        
    }

    if (userChoice === "paper") {

        if (computerChoice === "scissors") {

            return "The winner is the computer";
        
        } 
        
        return "The winner is the user";
        
    }

    if (userChoice === "scissors") {

        if (computerChoice === "rock") {

            return "The winner is the computer";
        
        } 
        
        return "The winner is the user";
        
    }

};

Hi Paul. It is implied in the code that the winner would be defaulted to the user. If you look…

if (userChoice === "rock") {

        if (computerChoice === "paper") {

            return "The winner is the computer";
        
        } 
        
        return "The winner is the user";
        
    }

If the computer choice is not paper (i.e. scissors) it will return the user as the winner. Hope this helps.

Yeah OK, I got that part. but why didn’t the coder use an ‘else’ block for the default return statement ?

Once a return statement is encountered, the function returns and execution ends.

I’m wondering why the default return statement was not wrapped in an ‘else’ block ?

Why should it be? If the condition is true, the return inside the if is used, otherwise the default return is used.

Nobody can really answer that except the person that wrote it. It’s just a matter of style. You can have a default value and handle all other values before it, or you can explicitly handle all values. Some would say it’s more elegant with a default, some would say it’s not always explicit enough (the amount of code that comes before a default can be a determining factor).

Sort of like using the default case in a switch or a default return value in a function.

function isTrue(test) {
  if (test) {
    return true;
  }
  return false;
}
function isTrue(test) {
  if (test) {
    return true;
  } else {
    return false;
  }
}