Rock, Paper, Scissors in JavaScript

前端 未结 13 1713
野的像风
野的像风 2020-12-03 00:13

I\'m working on making my first game (Rock Paper Sissors) and I ran into an issue where when the userChoice is scissors and the com

13条回答
  •  长情又很酷
    2020-12-03 00:42

    You were unable to see the issue most likely due to poor indentation of your code. Properly indented the issue is clear:

    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";
                }
            }
        }
    }
    

    Your if (choice1 === "scissors") { is within if (choice1 === "paper") {. The code within will never be reached.

提交回复
热议问题