第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何使用內(nèi)部函數(shù)更改外部變量的值?

如何使用內(nèi)部函數(shù)更改外部變量的值?

紫衣仙女 2023-04-20 16:19:06
在下面的代碼中,當(dāng)我使用函數(shù) calcScore 時(shí),外部變量 playerScore 和 computerScore 沒有更新?如何使用函數(shù)更新它?據(jù)我了解,可以從內(nèi)部范圍更改外部變量的值,但為什么它在這里不起作用?    <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貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

Javascript 不會通過引用傳遞變量 - 因此您對函數(shù)所做的任何修改playerScore和computerScore內(nèi)部修改calcScore()都只是該函數(shù)的局部修改。


您可以做的是制作playerScore全局computerScore變量。這樣任何修改都將在全局范圍內(nèi)?;蛘?,您可以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>


查看完整回答
反對 回復(fù) 2023-04-20
?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

你兩次定義了這兩個(gè)變量,所以現(xiàn)在我很困惑。您是否希望能夠在游戲函數(shù)中重用這些變量而無需重新定義它們?因?yàn)槿绻沁@樣的話,那么它們應(yīng)該是全局變量而不是函數(shù)參數(shù)(您將無法在該函數(shù)之外使用函數(shù)參數(shù)中定義的變量)。



查看完整回答
反對 回復(fù) 2023-04-20
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號