简体   繁体   English

条件语句未按预期运行:Javascript

[英]Conditional statement not functioning as expected: Javascript

I am trying to make a small rock, paper and scissors program between a user and the computer.我正在尝试在用户和计算机之间制作一个小的石头、剪纸和剪刀程序。 Everything seems to work fine except the gameRound() conditional statement.除了 gameRound() 条件语句外,一切似乎都运行良好。 It doesn't matter what the user input is.用户输入是什么并不重要。 It just runs the (else) statement.它只是运行 (else) 语句。

 // a function that determines what the computer's choice is
    const getComputerChoice = () => {
        let compChoice = Math.random();
    
        if(compChoice > 0.5) {
            return "Rock";
        } else if(compChoice < 0.5) {
            return "scissors";
        } else if(compChoice == 0) {
            return "paper";
        }
    }
    
    // prompts the user to enter rock, paper or scissors
    let playerSelection = prompt();
    let computerSelection = getComputerChoice();
    
    // the logic that decides who wins and loses
    const gameRound = (playerSelection, computerSelection) => {
        if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
                return "It's a tie!!!";
        }  else if(playerSelection === "rock" && computerSelection === "paper")  {
                return "Computer wins!!!";
        }  else if(playerSelection === "rock" && computerSelection === "scissors")  {
                return "You win!!!";
        }  else if(playerSelection === "paper" && computerSelection === "scissors")  {
                return "Computer wins!!!";
        }  else if(playerSelection === "paper" && computerSelection === "rock")  {
                return "You win!!!";
        }  else if(playerSelection === "scissors" && computerSelection === "rock")  {
                return "Computer wins!!!";
        }  else if(playerSelection === "scissors" && computerSelection === "paper")  {
                return "You win!!!";
        }  else {
            return "Winner undecided";
        }
    }
    
    // the function that begins the game
    const game = () => {
        for(let i = 0; i < 5; i++) {
            return gameRound();
        }
    }
    
    console.log(game());

gameRound conditional statement doesn't function as expected due to the newly declared parameters playerSelection , computerSelection .由于新声明的参数playerSelectioncomputerSelectiongameRound条件语句无法按预期运行。 So it has to be a function statement rather than the inline arrow function.所以它必须是一个函数语句而不是内联箭头函数。

Then, Javascript code would be as follows:然后,Javascript代码如下:

// a function that determines what the computer's choice is
const getComputerChoice = () => {
    let compChoice = Math.random();

    if(compChoice > 0.5) {
        return "Rock";
    } else if(compChoice < 0.5) {
        return "scissors";
    } else if(compChoice == 0) {
        return "paper";
    }
}

// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
    if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
            return "It's a tie!!!";
    }  else if(playerSelection === "rock" && computerSelection === "paper")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "rock" && computerSelection === "scissors")  {
            return "You win!!!";
    }  else if(playerSelection === "paper" && computerSelection === "scissors")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "paper" && computerSelection === "rock")  {
            return "You win!!!";
    }  else if(playerSelection === "scissors" && computerSelection === "rock")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "scissors" && computerSelection === "paper")  {
            return "You win!!!";
    }  else {
        return "Winner undecided";
    }
}

// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();

for (let i = 0; i < 5; i++) {
    console.log(gameRound(playerSelection, computerSelection));
}

Or if you mean to play 5 rounds of the rock-paper-scissors, the allocation of playerSelection and computerSelection variables should be inside the for loop as follows:或者如果你的意思是玩 5 轮剪刀石头布,那么playerSelectioncomputerSelection变量的分配应该在for循环内,如下所示:

// a function that determines what the computer's choice is
const getComputerChoice = () => {
    let compChoice = Math.random();

    if(compChoice > 0.5) {
        return "Rock";
    } else if(compChoice < 0.5) {
        return "scissors";
    } else if(compChoice == 0) {
        return "paper";
    }
}

// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
    if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
            return "It's a tie!!!";
    }  else if(playerSelection === "rock" && computerSelection === "paper")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "rock" && computerSelection === "scissors")  {
            return "You win!!!";
    }  else if(playerSelection === "paper" && computerSelection === "scissors")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "paper" && computerSelection === "rock")  {
            return "You win!!!";
    }  else if(playerSelection === "scissors" && computerSelection === "rock")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "scissors" && computerSelection === "paper")  {
            return "You win!!!";
    }  else {
        return "Winner undecided";
    }
}

for (let i = 0; i < 5; i++) {
    // prompts the user to enter rock, paper or scissors
    let playerSelection = prompt();
    let computerSelection = getComputerChoice();
    console.log(gameRound(playerSelection, computerSelection));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM