Else if error


#1

Hi everybody!

So i first ran this version of code for my rock/paper/scissors program.

It works perfectly.

var userChoice = Math.random();
if (userChoice < 0.34) {
    userChoice = "rock";
} else if(userChoice <= 0.67) {
    userChoice = "paper";
} else {
    userChoice = "scissors";
}

console.log(`user choice is ${userChoice}`);

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

console.log(`computer choice is ${computerChoice}`);

function compare(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return "it's a tie!";
  } if (userChoice === "rock" && computerChoice !== "paper") {
    return "user win";
    } else {
    return "computer win";
  } if (userChoice === "paper" && computerChoice !== "scissors") {
    return "user win";
    } else {
    return "computer win";
  } if (userChoice === "scissors" && computerChoice !== "rock") {
    return "user win";
    } else {
    return "computer win";   
  }
}

compare(userChoice, computerChoice);
console.log(compare(userChoice, computerChoice));

Then i wanted to make it look a bit nicer; so i replaced my nested If with else if.

var userChoice = Math.random();
if (userChoice < 0.34) {
    userChoice = "rock";
} else if(userChoice <= 0.67) {
    userChoice = "paper";
} else {
    userChoice = "scissors";
}

console.log(`user choice is ${userChoice}`);

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

console.log(`computer choice is ${computerChoice}`);

function compare(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return "it's a tie!";
  } else if (userChoice === "rock" && computerChoice !== "paper") {
    return "user win";
    } else {
    return "computer win";
  } else if (userChoice === "paper" && computerChoice !== "scissors") {
    return "user win";
    } else {
    return "computer win";
  } else if (userChoice === "scissors" && computerChoice !== "rock") {
    return "user win";
    } else {
    return "computer win";   
  }
}

compare(userChoice, computerChoice);
console.log(compare(userChoice, computerChoice));

But when i use else if; the console is sending an error :frowning: i don’t get why!

  } else if (userChoice === "paper" && computerChoice !== "scissors") {
    ^^^^
SyntaxError: Unexpected token else

Can someone please explain me what i did wrong?
Additionnaly; should i use my console.log at the end of the code or am i fine using it this way? (for best practice purpose).

Thank you!


#2

I don’t have much understanding about JavaScript but I can tell you that putting an else(else if) statement after another else statement looks kinda strange.
Else if’s aren’t important here(except if you think they let it look cleaner) as they are generally used to stop checking the following conditions if the current condition is already true. The return already does that, so in my opinion there’s no need for it.

I will try to show you what is happening here.

if (condition1) {
  //Code
}
else {
  if (condition2) {
    //Code
  }
  else {
    //Code
  }
}

is equivalent to:

if (condition1) {
  //Code
} else if {
  //Code
} else {
  //Code
}

If you write:

if (condition1) {
  //Code
} else if (condition2) {
  //Code
  } else {
  //Code
} else if (condition3) {
  //Code
}

you would be saying:

if (condition1) {
  //Code
}
else {
  if (condition2) {
    //Code
  }
  else {
    //Code
  }
  else {
    if (condition3) {
      //Code
    }
  }
}

You see those else’s in front of each other? I think they are the evil doers here.

I may be wrong.

if (userChoice === computerChoice) {
    return "it's a tie!";
  } else if (userChoice === "rock" && computerChoice !== "paper") {
    return "user win";
    } else {
    return "computer win";
  } else if (userChoice === "paper" && computerChoice !== "scissors") {

This part is no different from:

if (userChoice === computerChoice) {
    return "it's a tie!";
  } else if (userChoice === "rock" && computerChoice !== "paper") {
    return "user win";
  } else {
    return "computer win";
  } else if (userChoice === "paper" && computerChoice !== "scissors") {

What changes is the indentation. And you can’t put an else if after an else in most programming languages as that would just be nonsense.

EDIT: Are you sure the first version of the code works? What happens when user chooses scissors and computer chooses rock? My Gedankenexperiment said that your program would say that the computer won. Why?

if (userChoice === computerChoice) {
    return "it's a tie!";
  } if (userChoice === "rock" && computerChoice !== "paper") {
    return "user win";
    } else {
    return "computer win";

userChoise is “scissors” and computerChoise is"paper". As the userChoice isn’t “rock”, the first else if statement is false. Because of this, it will jump to the else statement that says to return “computer win”. That doesn’t sound right. The user should have won! The reason I am telling you this is that you are using a random number generator which makes it not impossible for that event to happen(you may not notice it). I might also be wrong about that one, because I didn’t test it.

EDIT3: The output is wrong if the user chooses scissors and the computer chooses paper and when the user chooses paper and the computer chooses rock. So the probability of it giving a wrong result is 2 in 9, which is about 22%, if I’m correct.

EDIT4: I’ve tested it. Yep. The first example gives the wrong result. You should look at your code and see how you can fix it.


#3
const choices = ['rock', 'paper', 'scissors']

const getRandomChoice () => choices[Math.floor(Math.random() * 3)];

var userChoice = getRandomChoice()
var computerChoice = getRandomChoice()

That would clean things up considerably and rid the need of any if…else if…else logic.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.