Help needed for JS rock-paper-scissors game winner logic

I’m creating a simple game of rock paper scissors in JavaScript, but I’m having trouble determining the winner when the user chooses scissors and the computer chooses rock. My code works for all other combinations.

Here’s what I have:

var userChoice = prompt("Do you choose rock, paper, or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else if (choice2 === "scissors") {
            return "scissors wins";
        }
    }
    if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else if (choice2 === "paper") {
            return "scissors wins";
        }
    }
};

console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);

Can anyone help me fix the issue so it correctly identifies the winner for all scenarios?

Hello!

To fix the compare function, make sure it returns the result rather than just running the logic without capturing the output. Here’s a quick update for your code:

  1. Modify your function so it returns a value.
  2. Update your function calls to properly log the result.

For example:

var result = compare(userChoice, computerChoice);
console.log(result);

This will ensure the output of your comparison is properly logged and visible. Let me know if this helps!

Hey everyone! Here’s a streamlined version of the compare function that enhances readability and reduces redundancy by utilizing a single return statement for each outcome:

var compare = function(choice1, choice2) {
    if (choice1 === choice2) return "The result is a tie!";
    return (choice1 === "rock" && choice2 === "scissors") ||
           (choice1 === "paper" && choice2 === "rock") ||
           (choice1 === "scissors" && choice2 === "paper") 
           ? choice1 + " wins" 
           : choice2 + " wins";
};

This format makes it clearer to follow the logic while maintaining the functionality of the original code.

Happy coding!

Hello everyone!

Using a switch statement is a great way to enhance the clarity and organization of your compare function. Here’s how you can structure it:

var compare = function(choice1, choice2) {
    switch (choice1) {
        case choice2:
            return "The result is a tie!";
        case "rock":
            return (choice2 === "scissors") ? "rock wins" : "paper wins";
        case "paper":
            return (choice2 === "rock") ? "paper wins" : "scissors wins";
        case "scissors":
            return (choice2 === "paper") ? "scissors wins" : "rock wins";
        default:
            return "Invalid choice!";
    }
};

By implementing this solution, your game can accurately determine the winner for all possible combinations of user and computer choices in the rock-paper-scissors JavaScript game. Happy coding!