在JavaScript中的岩石,纸,剪刀

时间:2014-01-06 23:18:25

标签: javascript

我正在上一个代码学课程(找到here),但一直告诉我“你的代码返回'摇滚胜利'而不是'纸张胜利',当输入是纸和岩石时”,为什么?应该是正确的。因为它在谈论'摇滚胜利',所以它谈论的是摇滚与剪刀。那么,为什么在“摇滚胜利”的唯一结果中甚至没有纸张参与时,它会说“而不是纸张胜利”?

var compare = function (choice1, choice2) {  

    if (choice1 === choice2) {
        return("The result is a tie!");
    }

    if (choice1 === "rock") {
        if (choice2 === "scissors");
    } else {
        return ("rock wins");
    }

    if (choice1 === "paper") {
        if (choice2 === "rock");
    } else {
        return ("paper wins");
    }

    if (choice1 === "paper") {
        if (choice2 === "scissors");
    } else {
        return ("scissors wins");
    }

};

5 个答案:

答案 0 :(得分:3)

看看你的第一个条件:

if (choice1 === "rock") {
    if (choice2 === "scissors");
} else {
    return ("rock wins");
}

因此,如果choice1摇滚,则输入if - 块(实际上并不返回任何内容,但因为在这种情况下choice1实际上是"paper"进入else - 块,无条件地返回"rock wins"。尝试重构它是这样的:

if (choice1 === choice2) {
    return("The result is a tie!");
}

if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return ("rock wins");
    } else {
        return ("paper wins");
    }
}

if (choice1 === "paper") {
    if (choice2 === "rock") {
        return ("paper wins");
    } else {
        return ("scissors wins");
    }
}

if (choice1 === "paper") {
    if (choice2 === "scissors") {
        return ("scissors wins");
    } else {
        return ("rock wins");
    }
}

但是,嘿,让我们开心。尝试将您的选择放入数组中:

var choices = ["rock", "paper", "scissors"];

现在,请注意右边的项目总是击败左边的项目(如果我们认为数组包裹了)。我们如何使用它来简化代码?那么我们可以比较每个选择的指标,注意处理剪刀与岩石的边缘情况:

var x = choices.indexOf(choice1),
    y = choices.indexOf(choice2);
if (x === y) {
    return("The result is a tie!");
} else if (x > y) {
    if (x == 3 && y == 0) {
        return choice2 + " wins";
    } else {
        return choice1 + " wins";
    }
} else {
    return choice2 + " wins";
}

但我们可以在此使用remainder operator%)来更轻松地处理边缘情况:

var choices = ["rock", "paper", "scissors"];
var compare = function (choice1, choice2) {
    var x = choices.indexOf(choice1),
        y = choices.indexOf(choice2);
    if (x === y) {
        return("The result is a tie!");
    }

    return (((x - y) % 3) > 0 ? choice1 : choice2) + " wins";
}

答案 1 :(得分:1)

当choice1不是“摇滚”时,你的功能总会返回“摇滚胜利”。这是因为你使用了if - else语句。

你在做什么: 如果choice1是摇滚做某事 否则返回“摇滚胜利”

我会给你第一个声明:

    if (choice1 === "rock") {
        if (choice2 === "scissors") return ("rock wins");
        if (choice2 === "paper") return ("Paper wins");
    }

答案 2 :(得分:1)

jsFiddle Demo

使用if语句时有一些有趣的选择。他们之后不应该有半冒号。此外,当使用许多if then else语句时,逻辑组合可能会很困难。通常,在这些情况下最好使用switch case statementMDN

var compare = function (choice1, choice2) {
 if(choice1==choice2)return "The result is a tie!";
 switch(choice1+choice2){
    case "rockscissors": case "scissorsrock":
        return "rock wins";
    case "rockpaper": case "paperrock":
        return "paper wins";
    default: return "scissors wins";
 }
};

答案 3 :(得分:0)

if (choice1 === "rock") {
    if (choice2 === "scissors");
} else {
    return ("rock wins");
}

再看一遍。你说:

IF choice1 === rock,那么     如果选择2 ===剪刀然后没有 ELSE(choice1不是摇滚)     返回'摇滚胜利'

这是明确的括号有用的情况。我猜你打算这样做:

if (choice1 === "rock") {
    if (choice2 === "scissors") {
    }
} else {
    return ("rock wins");
}

答案 4 :(得分:0)

如果数据受到严格控制,您可以这样做:

  

如果在( choice1 +“”+ choice2 )中找到“ ks ”,“ rr ”或“ sp ”,您 choice1   赢了,否则输了(例子如下)

function getWinner(choice1, choice2){
    var both_str, after_removing;

    if(choice1 == choice2){
        return "The result is a tie!";
    }

    both_str = (choice1 + "" + choice2);
    after_removing = both_str.replace(RegExp("ks|rr|sp", "g"), "");

    return (choice1 + ((both_str.length - after_removing.length) ? " won" : " lost"));
}

你得到以下结果:

console.log(getWinner("scissors", "paper")); //scissors won
console.log(getWinner("rock", "scissors")); //rock won
console.log(getWinner("paper", "rock")); //paper won

console.log(getWinner("scissors", "rock")); //scissors lost
console.log(getWinner("rock", "paper")); //rock lost
console.log(getWinner("paper", "scissors")); //paper lost

console.log(getWinner("scissors", "scissors")); //The result is a tie!
console.log(getWinner("rock", "rock")); //The result is a tie!
console.log(getWinner("paper", "paper")); //The result is a tie!