如何在调用函数时将函数中的提示值作为参数传递给同一函数?

问题描述 投票:0回答:1

我有一个函数 playRound(),它接受两个参数,即通过提示的playerSelection 和computerSelection,并对它们进行比较。 //基本剪刀石头布

第二个函数 game() 调用 playRound(playerSelection, ComputerSelection) 来运行一次执行的比较。

我希望保存提示和计算机的值并用于循环中的一次执行,然后提示用户和计算机为以下每个循环输入新值。

我知道提示应该在 playRound() 内定义,computerSelection 也应该如此,但是当我在那里定义它并尝试将它们作为参数调用时,我收到错误。

我尝试了几种不同的实现,使用全局定义的变量,在 playRound() 内部并直接作为 playRound() 的参数,但结果与游戏不一致。

下面是我最新的实现,其中全局定义了变量,但这仅运行一次。

    const choices = ['rock', 'paper', 'scissors'];
    let user = prompt('Your selection: ').toLowerCase();
    let computer = getComputerChoice();

function getComputerChoice() {
    
    let randomChoice = Math.floor(Math.random()*choices.length);
    
    if (randomChoice === 2) {
        return choices[2];
    } else if (randomChoice === 1){
        return choices[1];
    } else {
        return choices[0];
    }
    
}

function playRound(playerSelection, computerSelection){

    //check for a tie
    if (playerSelection === computerSelection) {
        return('its a tie');
        
    } 
    
    //player choices rock
    if (playerSelection === choices[0]){
        if (computerSelection === choices[1]){
            return('You Lose! Paper beats rock.');
        } else if (computerSelection === choices[2]){
            return('You Win! Rock beats scissors.');
        }
    } 
    
    //player chooses paper
    if (playerSelection === choices[1]){
        if (computerSelection === choices[2]){
        return('You Lose! Scissors beats paper.');
        } else if (computerSelection === choices[0]){
            return('You Win! Paper beats rock.')
        }
    } 
    
    //player chooses scissors
    if (playerSelection === choices[2]){
        if (computerSelection === choices[0]){
            return('You Lose! Rock beats scissors.');
        } else if (computerSelection === choices[1]){
            return('You Win! Scissors beats paper.');
        }   
    } 
}


function game(){
    let playerScore = 0;
    let compScore = 0;


    
    while (playerScore < 4 || compScore < 4){
        const round = playRound( user, computer);

        if (round == 'its a tie'){
            return round;
        }

        if ( round.slice(0, 8) == 'You Win!'){
            ++playerScore;
            console.log('player score: ' + playerScore);
        } else if (round.slice(0, 9) == 'You Lose!'){ 
            ++compScore;
            console.log('compScore: ' + compScore);
        }

        if (playerScore == 3){
            return console.log('Player Wins!');
        } else if (compScore == 3){
            return console.log('Computer Wins!');
        }
    }

}

game();
javascript function variables prompt
1个回答
0
投票

您可能需要添加一个 while 循环来循环回合,直到游戏结束。

const choices = ['rock', 'paper', 'scissors'];
let user = prompt('Your selection: ').toLowerCase();
let computer = getComputerChoice();

while () {
    //code here
    ...
    if (playerScore == 3){
            return console.log('Player Wins!');
            break;
    } else if (compScore == 3){
            return console.log('Computer Wins!');
            break;
    }
}

或者,

while (choices != NULL) {
    //code here
    ...
    if (playerScore == 3){
            return console.log('Player Wins!');
            choices = NULL;
    } else if (compScore == 3){
            return console.log('Computer Wins!');
            choices = NULL;
    }
}

我目前无法对此进行测试,但如果这不起作用,也许循环整个代码(包括用户提示)可能会起作用。

希望这有帮助。

干杯。

© www.soinside.com 2019 - 2024. All rights reserved.