为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

紫衣仙女 2023-04-20 16:19:06
在下面的代码中,当我使用函数 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;            }        }
查看完整描述

2 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

Javascript 不会通过引用传递变量 - 因此您对函数所做的任何修改playerScore和computerScore内部修改calcScore()都只是该函数的局部修改。


您可以做的是制作playerScore全局computerScore变量。这样任何修改都将在全局范围内。或者,您可以calcScore()返回修改后的值。


使用全局方法:


    <script>

        let playerScore = 0;

        let computerScore = 0;


        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) {

            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;

            }

        }

        function game() {


            for (let i = 1; i <= 5; i++) {

                let result = playRound(prompt("Select rock, paper, or scissors!"), computerSelection());

                calcScore(result);

                console.log(`You have ${playerScore} points! Computer has ${computerScore} points!`);

            }

            playerScore, computerScore = 0, 0;

        }

    </script>

替代方法:


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

            }


            return [playerScore, computerScore];

        }

        function game() {

            let playerScore = 0;

            let computerScore = 0;

            for (let i = 1; i <= 5; i++) {

                let result = playRound(prompt("Select rock, paper, or scissors!"), computerSelection());

                let scores = calcScore(result, playerScore, computerScore);

                playerScore += scores[0];

                computerScore += scores[1]; 

                console.log(`You have ${playerScore} points! Computer has ${computerScore} points!`);

            }

            playerScore, computerScore = 0, 0;

        }

    </script>


查看完整回答
反对 回复 2023-04-20
?
撒科打诨

TA贡献1934条经验 获得超2个赞

你两次定义了这两个变量,所以现在我很困惑。您是否希望能够在游戏函数中重用这些变量而无需重新定义它们?因为如果是这样的话,那么它们应该是全局变量而不是函数参数(您将无法在该函数之外使用函数参数中定义的变量)。



查看完整回答
反对 回复 2023-04-20
  • 2 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信