0

我刚开始学习 JavaScript(实际上是今天),我非常感谢嵌套 if-else 语句的一些帮助。我想我会写一个简单的程序来练习,而且似乎我的 if 块中的每个 if-else 语句都会执行,而不管我输入哪个参数。任何与问题无关的指针甚至是你注意到的东西手被赞赏。再次感谢。我的代码如下。

编辑:我现在已经明白了,并且了解了我的方式的错误。感谢所有如此迅速地发表评论和提供建议的人。

var playerOne = prompt('Choose rock, paper, or scissors'); 
var playerTwo = prompt('Choose rock, paper, or scissors'); 

var fight = function (playerOne, playerTwo)
{ 
    if( playerOne == 'rock' || 'Rock')
    { 
        if (playerTwo == 'paper' || 'Paper')
        {
            alert('Player Two Wins!');
        }
        else if (playerTwo == 'rock' || 'Rock')
        {
            alert('Tie!');
        }
        else
        {
            alert('Player One wins!');
        }
    }

        if(playerOne == 'paper' || 'Paper')
        { 
            if (playerTwo == 'paper' || 'Paper')
            {
                alert('Tie!');
            }
            else if (playerTwo == 'rock' || 'Rock')
            {
                alert('Player One Wins!'); 
            }
            else 
            {
                alert('Player Two  wins!');
            }
        }

        if (playerOne == 'scissors' || 'Scissors')
        {
             if (playerTwo == 'paper' || 'Paper')
             {
                alert('Player One Wins!');
             }
            else if (playerTwo == 'rock' || 'Rock')
            {
                alert('Player Two Wins!');
            }
            else
            {
                alert('Tie!');
            }
        }

};

fight(playerOne, playerTwo); 
4

3 回答 3

2

正如一些人指出的那样,您的if陈述需要采用以下形式:

if (playerOne == 'paper' || playerOne == 'Paper')

或更简洁:

if (playerOne.toLowerCase() == 'paper')

问题是它playerOne == 'paper' || 'Paper'总是会返回一个“Truthy”值(有关 Truthy 和 Falsy 值的更多详细信息,请参见http://11heavens.com/falsy-and-truthy-in-javascript)。

顺便说一句,虽然多个语句绝对没有问题if,但如果我编写这个练习,我的方式将涉及更少的if语句(看起来有点像这样:

var playerOne = prompt('Choose rock, paper, or scissors');
var playerTwo = prompt('Choose rock, paper, or scissors');
var fists = {
    "rock": {
        "beats": "scissors",
        "loses": "paper"
    },
    "paper": {
        "beats": "rock",
        "loses": "scissors"
    },
    "scissors": {
        "beats": "paper",
        "loses": "rock"
    }
}

var fight = function (playerOne, playerTwo) {
    playerOne = playerOne.toLowerCase();
    playerTwo = playerTwo.toLowerCase();

    if (fists[playerOne] === undefined || fists[playerTwo] === undefined) {
        alert('Someone threw an unknown fist!');
    } else if (fists[playerOne].beats === playerTwo) {
        alert('Player One wins!');
    } else if (fists[playerTwo].beats === playerOne) {
        alert('Player Two Wins!');
    } else {
        alert('Tie!');
    }
};

fight(playerOne, playerTwo);

通过客观化石头/纸/剪刀组合,IMO 代码更容易阅读。

于 2013-07-03T17:25:50.153 回答
1

其他评论和答案都很棒,所以我不会重复他们所说的。但你征求意见,我的意见是不要if一开始就使用这么多陈述。编程的很大一部分是学习如何减少不必要或重复的代码。对象和数组之类的数据结构对此很有用:

var win_conditions = {    //simple object showing which hands beat which
    'rock': 'scissors',
    'paper': 'rock',
    'scissors': 'paper'
}

var fight = function(p1, p2) {
    var result;

    if (!win_conditions.hasOwnProperty(p1) || !win_conditions.hasOwnProperty(p2)) {
        result = false;       //error! user typed something invalid
    } else {
        if (win_conditions[p1] == p2) {
            result = 'Player One wins!';
        } else if (win_conditions[p2] == p1) {
            result = 'Player Two wins!';
        } else {
            result = 'Tie!';
        }
    }

    return result;
}

var fight_result = false;
var prompt_text = 'Choose rock, paper, or scissors';
var playerOne = prompt(prompt_text); 
var playerTwo = prompt(prompt_text); 

//keep asking until the user types a valid option
while (!fight_result) {
    fight_result = fight(playerOne.toLowerCase(), playerTwo.toLowerCase());
}
alert(fight_result);
于 2013-07-03T17:28:51.910 回答
0

我推荐你使用FireBug来调试你的 JavaScript 代码。当您调试代码时,将其更改alert()console.log()并且可能更改playerOne/playerTwo为简单字符串。

(不完全适合您的问题,但如果您继续学习,总体而言这是一个很好的建议。)

于 2013-07-03T17:04:27.307 回答