繁体   English   中英

退出Javascript函数。 简单的石头,纸,剪刀游戏

[英]Exiting out of a function in Javascript. Simple rock, paper, scissors game

如果userChoice为null或除了石头,纸或剪刀之外的单词,如何停止执行该函数。 我尝试使用return,但是无法正常工作。 任何帮助表示赞赏。 谢谢

JS小提琴链接= http://jsfiddle.net/Renay/d9bea2ra/1/

var userChoice = prompt('Do you choose rock, paper or scissors?');
var compChoice = Math.random();

if (compChoice <= 0.34) {
    compChoice = 'rock';
} else if (compChoice <= 0.67) {
    compChoice = 'paper';
} else {
    compChoice = 'scissors';
}

function compare() {
    if (userChoice == compChoice) {
        console.log( 'The result is a tie!');
    } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
        console.log( 'You win!');
    } else {
        console.log('You lose!');
    }

    if (userChoice === null) {
        console.log('Please select an option');     
    } else if (userChoice !== 'rock'&&'paper'&&'scissors') {
        console.log('Please select rock, paper or scissors');    
    }
}

console.log('Your choice = ' + userChoice);
console.log('Computer Choice = ' + compChoice);
compare();

if语句中的条件错误。 它应该是:

if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors')

如果e1 && e2 && e3 && ...的形式的表达式都为真,则计算最后一个eN子表达式。 因此,您的测试相当于:

if (userChoice !== 'scissors')

您应该显示游戏结果之前放下该支票,然后从函数中返回。 因此应该是:

function compare() {
    if (userChoice === null) {
        console.log('Please select an option');    
        return; 
    } else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') {
        console.log('Please select rock, paper or scissors');   
        return; 
    }

    if (userChoice == compChoice) {
        console.log( 'The result is a tie!');
    } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
        console.log( 'You win!');
    } else {
        console.log('You lose!');
    }

}

只需return;

function compare() {
  if(!userChoice)
     return;
  // (...) more code
}

return调用退出该函数。 但请注意,您只能在function内部使用return

如果我理解正确,您想返回代码的开头。 您应该将整个内容包装在一个函数中,然后再次调用该函数:

function rockPaperScissors() {
    var userChoice = prompt('Do you choose rock, paper or scissors?');
    var compChoice = Math.random();

    if (compChoice <= 0.34) {
        compChoice = 'rock';
    } else if (compChoice <= 0.67) {
        compChoice = 'paper';
    } else {
        compChoice = 'scissors';
    }

    function compare() {
        if (userChoice == compChoice) {
            console.log( 'The result is a tie!');
        } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper') ) {
            console.log( 'You win!');
        } else {
            console.log('You lose!');
        }

        if (userChoice === null) {
            console.log('Please select an option');
            rockPaperScissors();
        } else if (userChoice !== 'rock' && userChoice !== 'paper' && userChoice !== 'scissors') { // fixed
            console.log('Please select rock, paper or scissors'); 
            rockPaperScissors();
        }
    }

    console.log('Your choice = ' + userChoice);
    console.log('Computer Choice = ' + compChoice);
    compare();
}
rockPaperScissors();

在检查它是否不是石头,纸张或剪刀之一时,您也遇到了错误,因此我将其修复。 看来Barmar在我之前就了解了 。)

我更新了函数的每个if / else部分,使其在与if / else条件匹配且return false时退出。 另外,我将if (userChoice === null)检查更改为if (userChoice === "")因为它看起来像是prompt('Do you choose rock, paper or scissors?'); 当用户不输入任何值时,返回一个空字符串。 小提琴同样,我刚刚更新了该函数的开头,以根据** Barmar的建议(他第一次就正确了)进行工作。

function compare() {
    if (userChoice === "") {
        console.log('Please select an option');
        return false;
    } else if ((userChoice !== 'rock') && (userChoice !=='paper') && (userChoice !=='scissors')) {
        console.log('Please select rock, paper or scissors');
        return false;
    }

    if (userChoice == compChoice) {
        console.log('The result is a tie!');
        return false;
    } else if ((userChoice == 'rock' && compChoice == 'scissors') || (userChoice == 'paper' && compChoice == 'rock') || (userChoice == 'scissors' && compChoice == 'paper')) {
        console.log('You win!');
        return false;
    } else {
        console.log('You lose!');
        return false;
    }
}

暂无
暂无

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

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