如果Javascript不符合特定条件,我将如何停止它?

时间:2015-04-30 20:15:54

标签: javascript

我正在从Codecademy学习Javascript,我正在研究额外的问题,看看我能否做到。我正在研究石头剪刀游戏,如果用户选择了除岩石,纸张或剪刀之外的选项,我试图让它出错。我设法得到它来提取错误消息,但脚本将继续运行,计算机仍将选择三个中的一个。

如果用户键入除了石头纸或剪刀之外的其他内容并且仅返回错误消息而不是返回消息然后继续显示计算机的选择,我将如何停止程序?

var userChoice = prompt("Do you choose rock, paper or scissors?");

if  (userChoice !== "rock" || "scissors" || "paper") {
    console.log("That is not one of the options");
}

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer: " + computerChoice);


var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}

compare(userChoice, computerChoice)

4 个答案:

答案 0 :(得分:3)

方法1

if  (userChoice !== "rock" && userChoice  !== "scissors" && userChoice  !== "paper") {
    console.log("That is not one of the options");
    return false;
}

方法2

switch(userChoice)
{
  case 'rock': break;
  case 'scissors': break;
  case 'paper': break;
  default: {
     console.log(userChoice + "That is not one of the options");
     return false;
  }
 }

在jsfiddle工作:enter link description here

答案 1 :(得分:0)

将它放入函数中并在出错时返回。

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        } else {
            return "rock wins";
        }
    }
}

var game = function() {
    var userChoice = prompt("Do you choose rock, paper or scissors?");

    if  (userChoice !== "rock" || "scissors" || "paper") {
        console.log("That is not one of the options");
        return;
    }

    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    console.log("Computer: " + computerChoice);
    compare(userChoice, computerChoice);
}

game();

答案 2 :(得分:0)

您可以使用例外,如下所示:Is it possible to stop JavaScript execution?

基本上, 触发错误消息后,执行:

throw new Error("Your Error Message!");

虽然这会结束整场比赛,但对于用户输入的拼写错误并不可取。它会有效地阻止该计划。

答案 3 :(得分:0)

如果fooToBar语句在函数内,请在if语句的末尾使用return false;

如果if语句在函数外部,则将剩余的代码包装在if子句中,这样如果初始条件成功,它将不会执行。