Javascript,不区分大小写,摇滚,纸张,剪刀

时间:2016-07-26 08:30:39

标签: javascript

我使用javascript制作了一个摇滚,纸张,剪刀游戏并完成了基本功能:摇滚节拍剪刀等但现在我试图让它不区分大小写的答案请帮助和谢谢。

如果有帮助,这是我的代码

var userChoice = prompt("Do you choose rock, paper or scissors?Type your answer.     Refresh page to start again!");//Initial dialogue
var computerChoice = Math.random();//computer chooses a random number
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
//Above: if computer chooses a number it is equal to rock, paper or scissors
var compare = function(choice1, choice2)  //variable of computer and user choice
{
    if(choice1 === choice2) {
    return console.log("The result is a tie!)";//If the user and computer chooses the same then text
}
if(choice1 === "rock" ) {
    if(choice2 === "scissors") {
        return console.log("rock wins");
    } else {
        return console.log("paper wins");
    }
}
if(choice1 === "paper") {
    if(choice2 === "rock") {
        return console.log("paper wins");
    } else {
        if(choice2 === "scissors") {
            return console.log("scissors wins");
    }
}
if(choice1 === "scissors") {
    if(choice2 === "rock") {
        return console.log("rock wins");
    } else {
        if(choice2 === "paper") {
            return console.log("scissors wins");
        }
    }
}
}  //Abover: Rock beats scissors, paper beats rock, scissors beats paper and dialogue

console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);

1 个答案:

答案 0 :(得分:0)

要使JS不区分大小写,请将文本转换为小写:

var text = 'InPuT';

text = text.toLowerCase();

因此InPuT变为input,然后您可以测试它:

if(text=='input'){ //... }

希望它对你有帮助;)