2014-01-06 68 views
0

我参加了一个codecademy课程(找到了here),但一直告诉我“当代码是纸和摇滚时,你的代码返回'摇滚胜利'而不是'纸胜利',为什么?它应该是正确的。既然是在谈论“摇滚胜利”,那么它就是在谈论摇滚与剪刀。那么,为什么当“摇滚胜利”的唯一结果是甚至没有纸时,“而不是纸胜利”呢?JavaScript中的岩石,纸张,剪刀

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"); 
    } 

}; 
+3

'if(choice1 ===“paper”){if(choice2 ===“rock”);如果两个条件都是真的,或者你正在试图用这种方法做什么,那么你是不是该如何测试。阅读逻辑运算符:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators –

+0

也许这会帮助 - http://bit.ly/19NPQLh –

+0

问题后分号'if'语句。 – elclanrs

回答

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"; 
} 
+0

好的答案。如果在主要条件下,您可以考虑使用其他方法 –

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

在此再看看。你说:

IF选择1 ===岩石,THEN IF选择2 ===剪刀则不采取任何 ELSE(选择1不岩) 回归“摇滚胜”

这是一个情况下明显的括号有助于。我猜你的意思是这样:

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

你的函数总是返回“摇滚胜”的时候选择1是不是“摇滚”。这是因为你已经使用了if - else语句。

什么,你正在做的是: 如果选择1是岩石做一些 否则返回“摇滚胜”

我给你的第一条语句:

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

如果数据非常控制,你可以这样做:

如果(选择1 + “” + 选择2)你会发现 “KS”, “RR” 或 “SP”,你选择1 亿韩元,低于(例如其他丢失)

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! 
1

jsFiddle Demo

当使用if语句时,您会做出有意思的选择。后面不应该有分号。另外,当使用许多else 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"; 
} 
};