如何使用内部函数更改外部变量的值?

在下面的代码中,当我使用函数 calcScore 时,外部变量 playerScore 和 computerScore 没有更新?如何使用函数更新它?据我了解,可以从内部范围更改外部变量的值,但为什么它在这里不起作用?


    <script>

        function playRound(playerSelection, computerSelection) {

            playerSelection = playerSelection.toLowerCase();

            computerSelection = computerSelection.toLowerCase();

            if (playerSelection === computerSelection) {

                return "draw";

            }

            else if (playerSelection === "rock"){

                if (computerSelection === "scissors") return "win";

                else if (computerSelection === "paper") return "lose";

            }

            else if (playerSelection === "paper"){

                if (computerSelection === "scissors") return "lose";

                else if (computerSelection === "rock") return "win";

            }

            else if (playerSelection === "scissors"){

                if (computerSelection === "rock") return "lose";

                else if (computerSelection === "paper") return "win";

            }

        }

        

        function computerSelection() {

            let selection = ["rock", "paper", "scissors"];

            return selection[Math.floor(Math.random() * selection.length)];

        }

        function calcScore(result, playerScore, computerScore) {

            if (result === "win") {

                playerScore += 1;

                console.log("win");

            }

            else if (result === "lose") {

                computerScore += 1;

                console.log("lose");

            }

            else if (result === "draw") {

                playerScore += 1;

                computerScore += 1;

            }

        }



紫衣仙女
浏览 91回答 2
2回答

鸿蒙传说

Javascript 不会通过引用传递变量 - 因此您对函数所做的任何修改playerScore和computerScore内部修改calcScore()都只是该函数的局部修改。您可以做的是制作playerScore全局computerScore变量。这样任何修改都将在全局范围内。或者,您可以calcScore()返回修改后的值。使用全局方法:&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; let playerScore = 0;&nbsp; &nbsp; &nbsp; &nbsp; let computerScore = 0;&nbsp; &nbsp; &nbsp; &nbsp; function playRound(playerSelection, computerSelection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerSelection = playerSelection.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerSelection = computerSelection.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (playerSelection === computerSelection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "draw";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (playerSelection === "rock"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (computerSelection === "scissors") return "win";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (computerSelection === "paper") return "lose";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (playerSelection === "paper"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (computerSelection === "scissors") return "lose";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (computerSelection === "rock") return "win";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (playerSelection === "scissors"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (computerSelection === "rock") return "lose";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (computerSelection === "paper") return "win";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; function computerSelection() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let selection = ["rock", "paper", "scissors"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return selection[Math.floor(Math.random() * selection.length)];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function calcScore(result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result === "win") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("win");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (result === "lose") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("lose");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (result === "draw") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function game() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (let i = 1; i <= 5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = playRound(prompt("Select rock, paper, or scissors!"), computerSelection());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calcScore(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`You have ${playerScore} points! Computer has ${computerScore} points!`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore, computerScore = 0, 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>替代方法:&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function playRound(playerSelection, computerSelection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerSelection = playerSelection.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerSelection = computerSelection.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (playerSelection === computerSelection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "draw";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (playerSelection === "rock"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (computerSelection === "scissors") return "win";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (computerSelection === "paper") return "lose";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (playerSelection === "paper"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (computerSelection === "scissors") return "lose";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (computerSelection === "rock") return "win";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (playerSelection === "scissors"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (computerSelection === "rock") return "lose";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (computerSelection === "paper") return "win";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; function computerSelection() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let selection = ["rock", "paper", "scissors"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return selection[Math.floor(Math.random() * selection.length)];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function calcScore(result, playerScore, computerScore) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result === "win") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("win");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (result === "lose") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("lose");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (result === "draw") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerScore += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return [playerScore, computerScore];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function game() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let playerScore = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let computerScore = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (let i = 1; i <= 5; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let result = playRound(prompt("Select rock, paper, or scissors!"), computerSelection());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let scores = calcScore(result, playerScore, computerScore);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore += scores[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; computerScore += scores[1];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`You have ${playerScore} points! Computer has ${computerScore} points!`);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore, computerScore = 0, 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>

撒科打诨

你两次定义了这两个变量,所以现在我很困惑。您是否希望能够在游戏函数中重用这些变量而无需重新定义它们?因为如果是这样的话,那么它们应该是全局变量而不是函数参数(您将无法在该函数之外使用函数参数中定义的变量)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript