在函数中更新函数参数

Update function parameter withing function

本文关键字:函数 参数 更新      更新时间:2023-09-26

我已经用codecademy.com自学了一段时间的JS,其中一项任务是制作一个JS剪刀石头布游戏。最后,它要求一些改进,比如,如果它是一个领带,你如何使它可以再次选择,如果用户有一个无效的条目,你如何解决这个问题。

以下是整个JS游戏,相当小。我只需要一些帮助,如何通过正确检查第一个参数来获得我的函数中的第一个if语句正常工作,如果它无效,则提示更新参数而不影响第二个。然后重新运行函数。我的"或"操作符似乎也不起作用。

我也想知道是否有一个更干净/更好的方式来写我的第二个if语句。它工作得很好,我只是好奇是否有更好的方法。

谢谢大家的帮助!

var userChoice = prompt("Do you choose rock, paper or scissors?");
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 !== "rock" || "scissors" || "paper"){
       choice1 = prompt("Invalid entry, please reenter")}
     if (choice1 === choice2){ 
         var userChoice = prompt("Tie! Choose Again!");
         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);

    } 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 win"}
    } else if (choice1 === "scissors"){
        if (choice2 === "rock"){
            return "rock wins"
        }else {
            return "scissors wins"}
    };
};
compare(userChoice, computerChoice);

我为你做了一个小小提琴。看一下这段代码:

var validChoices = ['rock', 'paper', 'scissors'];
if (validChoices.indexOf(choice1) == -1){
   hoice1 = prompt("Invalid entry, please reenter")
   compare(choice1, choice2);
}

首先你说哪些选择是有效的。如果用户没有给出它们,您再次要求他做出选择,并再次调用您的函数,用户选择了新的choice1

关于你的条件:你所做的事情如下:

  • choice1 is rock

  • "scissors"为true

  • "paper"为真

由于javascript是弱类型的,所以字符串为真。要将它与你想要的方式进行正确的比较,请看看Curts的回答。

你的逻辑不正确。

您需要将if语句的第一行更改为:

if (choice1 !== "rock" || choice1 !== "scissors" || choice1 !== "paper"){

另外:

var options = ["rock", "scissors", "paper"];
if (options.indexOf(choice1)==-1){

由于第二个提示符必须再次检查,因此建议将检查合并到循环或外部函数中。例如:

var options = ['rock', 'paper', 'scissors'];
function getUserInput(){
    var txt= "Do you choose rock, paper or scissors?";
    while(true){ //loop until valid input or cancelled
        var res = prompt(txt);
        if(res==null)return null; //user cancelled
        var ind = options.indexOf(res.toLowerCase()); //alter input (in this case only tolowercase, but you could add trimming and such)
        if(ind >=0) return options[ind]; //if valid, return the stored value to be able to have the same value for comparison. You could choose to work with the indices too
        txt = "Invalid entry, please reenter";
    }
}

这也对输入执行一个toLowerCase,所以输入为'Rock'或'Rock'也将是有效的。

为了进一步避免语法差异,您可以使用类似

这样的内容来重用计算机输入的选项数组。
var computerChoice = options[ Math.floor(Math.random() * 3)];

这将使用选项数组中的字符串。Math.floor(Math.random() * 3)将生成一个整数0,1或2,其概率与<0.34等比较。


Edit:忍不住要添加一个例子来说明为什么使用索引本身是有用的。它将使代码能够与下一个元素进行比较(使用模返回到第一个元素),以便在选项顺序正确的情况下确定获胜者。

var options = ['rock', 'scissors', 'paper']; //ordered such that each value beats the next value (where the last value beats the first)
function getUserInput(txt){
    while(true){ //loop until valid input or cancelled
        var res = prompt(txt || "Do you choose rock, paper or scissors?"); //prompt with a pre defined text or the default text ( || makes sure undefined is replaced with the right hand value)
        if(res==null)return null; //user cancelled
        var ind = options.indexOf(res.toLowerCase()); //alter input (in this case only tolowercase, but you could add trimming and such)
        if(ind >=0) return ind; //if valid, return the index
        txt = "Invalid entry, please reenter";
    }
}

function runGame(promptText){
    var userChoice = getUserInput(promptText);
    if(userChoice===null)return; //user cancelled
    var computerChoice = Math.floor(Math.random() * 3); //generates a random number of 0,1 or 2 
    console.log("Computer: " + options[computerChoice]);
    if(userChoice === computerChoice)
        return runGame("Tie! Choose Again!"); //in case of Tie, start over with the prompText of Tie
    if(userChoice === (computerChoice + 1) % 3)   //the next value: +1 the % 3 (modulo 3) returns the remainder if the '+1' value is divided by 3 and thus turns 3 into 0   
        return options[computerChoice] + ' beats ' + options[userChoice]  + ' (Computer wins)';        
    return options[userChoice] + ' beats ' + options[computerChoice]  + ' (User wins)';        
}
var res = runGame();
if(res)
   alert(res);

小提琴