JavaScript无效输入条件

时间:2016-08-23 22:39:23

标签: javascript

我是JavaScript的新手。其他的一天我制作了一个Rock Paper Scissor游戏。游戏运行正常但我想知道你是否可以提出一个条件,如果用户选择输入无效选项,会出现一个警告框,说无效输入就是这样! 这是我的代码!

   var userchoice=prompt("Wanna play Rock, Paper and Scissors?");{
    if (userchoice!="paper") {
        alert("Invalid ID");
    }
    else if (userchoice!="scissors") {
        alert("Invalid ID");
    }
    else (userchoice!="rock") {
        alert("Invalid ID");
    }
}
var computerchoice=Math.random();{
    if(computerchoice<0.34){
    console.log(computerchoice="paper");
    }
    else if (computerchoice<=0.67) {
        console.log(computerchoice="rock");
    }
    else{
        console.log(computerchoice="scissors");
    }
};
var compare=function(choice1,choice2){
    if (choice1===choice2){
        console.log("The game was a tie!");
    }
    else if (choice1==="rock") {
        if (choice2==="scissors") {
            console.log("rock wins!");
        }
        else{
            console.log("paper wins!")
        }   
    }
    else if(choice1==="paper"){
        if (choice2==="rock") {
            console.log("paper wins!");
        }
        else{
            console.log("scissors wins!");
        }
    }
    else if (choice1==="scissors") {
        if (choice2==="paper") {
            console.log("scissors wins!")
        }
        else {
            console.log("rock wins!");
        }
    }
};

compare(userchoice,computerchoice);
console.log("User: "+userchoice);
console.log("Computer: "+computerchoice);
//End of Game

4 个答案:

答案 0 :(得分:0)

以下是一些压缩代码,用于确定任何选项是否无效。如果选择无效,它会发出警告,但您应该将其更改为更合适的行为。

var VALID_CHOICES = ['rock', 'paper', 'scissors']
var choice1 = 'rock', choice2 = 'something invalid'
var invalidChoice = false
;[choice1, choice2].forEach(function(choice) {
  if (VALID_CHOICES.indexOf(choice) === -1) invalidChoice = true
})
if (invalidChoice) alert('Picked an invalid choice')

答案 1 :(得分:0)

您可以使用Array.prototype.indexOf()。试试下面的演示:

&#13;
&#13;
function newGame(msg) {
    var allowed = ["rock","paper","scissors"],
        user = allowed.indexOf(prompt((msg||'Feeling lucky?') + ' Choose "rock", "paper" or "scissors":').trim().toLowerCase()),
        comp = Math.floor(Math.random()*3);

    if(user < 0) return newGame("Invalid ID!");

    console.log("You chose "+ allowed[user] +". Computer chose "+ allowed[comp] +".");

    if(user == comp)
        console.log('The game was a tie!');
    else if([1,-2].indexOf(comp-user) > -1)
        console.log('Computer wins with ' + allowed[comp] + '!');
    else
        console.log('You win with ' + allowed[user] + '!');
}
&#13;
<button onclick="newGame()">New game</button>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你可以有一个这样的函数,它会提示,但也会检查选择是否有效。

&#13;
&#13;
function ask(q) {
    var choice = prompt(q);
    if (choice != 'scissors' && choice != 'rock' && choice != 'paper') {
        alert('Invalid input. Only "scissors", "rock" or "paper" are allowed.');
        return false;
    } else {
        return choice;
    }
}
console.log('the choice is:',ask("Wanna play Rock, Paper and Scissors?"));
&#13;
&#13;
&#13;

if检查选项是否为"scissors""rock" or&#34; paper&#34;,如果无效则会提醒,并返回false。 但如果选择有效,它将返回choice

答案 3 :(得分:0)

  • 您可以按照您定义的方式compare
  • trim()在用户回答之前和之后删除空格
  • toLowerCase()将大写字母转换为小写

    var promptUser = function(text){
      var userAnswer = prompt(text);
      var cleanAnswer = userAnswer.trim().toLowerCase();
      if(['paper', 'scissors', 'rock'].indexOf(cleanAnswer) === -1) {
        alert('Invalid choice: ' + cleanAnswer);
      }
      return cleanAnswer;
    }
    
    var computerPlays = function() {
      var computerchoice = Math.random();
      if(computerchoice<0.34){
        computerchoice="paper";
      } else if (computerchoice<=0.67) {
        computerchoice="rock";
      } else {
        computerchoice="scissors";
      }
      return computerchoice;
    };
    
    var userChoice = promptUser("Wanna play Rock, Paper and Scissors?");
    var computerChoice = computerPlays();
    console.log('user picks:', userChoice);
    console.log('computer picks:', computerChoice)
    compare(userChoice,computerChoice);