简体   繁体   English

我的函数在第一条语句后不返回运行

[英]My function doesn't return run after first statement

So I'm making a Rock Paper Scissors function that takes to value and return: win lose or tied but is not returning anything after the first block.所以我正在制作一个 Rock Paper Scissors 函数,它需要价值和回报:赢输或平局,但在第一个块之后不返回任何东西。 Here's my code :这是我的代码:

function playRound(playerSelection, computerSelection){

   if(playerSelection === "rock" && computerSelection === "scissors") {
     return "wins";
    }
     else if(computerSelection === "paper") {
       return "lost"  
     }
       else{
       return "tied";
       }



if(playerSelection === "paper" && computerSelection === "rock") {
     return "wins";
    }
     else if(computerSelection === "scissors") {
       return "lost"  
     }
       else{
       return "tied";
       }



   if(playerSelection === "scissors" && computerSelection === "paper") {
     return "wins";
    }
     else if(computerSelection === "rock") {
       return "lost";
     }
       else{
       return "tied";
       }

   }

It's not returning anything because your logic is flawed.它没有返回任何东西,因为您的逻辑有缺陷。

Your logic is (from the first block):你的逻辑是(从第一个块):

  • case 1: playerSelection === "rock" && computerSelection === "scissors"案例1: playerSelection === "rock" && computerSelection === "scissors"
  • case 2: computerSelection === "paper"情况 2: computerSelection === "paper"
  • case 3: "everything else"案例3:“其他一切”

What I think you meant by your code is to actually have playerSelection === "rock" apply to the entire first block.我认为您的代码的意思是实际上让playerSelection === "rock"应用于整个第一个块。 So, you would need some nested if statements to capture that.因此,您需要一些嵌套的 if 语句来捕获它。

Your revised logic should resemble this:您修改后的逻辑应该类似于:

if ( playerSelection === "rock" ): if ( playerSelection === "rock" ):

  • case 1: computerSelection === "scissors"案例1: computerSelection === "scissors"
  • case 2: computerSelection === "paper"情况 2: computerSelection === "paper"
  • case 3: "everything else"案例3:“其他一切”

That being said, your check for "everything else" is a bit repetitive and there are some further optimizations that could be made.话虽如此,您对“其他一切”的检查有点重复,并且可以进行一些进一步的优化。 That being said, I've optimized the code a bit for you.话虽如此,我已经为您优化了代码。

function playRound(playerSelection, computerSelection) {
    if (playerSelection === computerSelection) {
        // may be worth validating that rock/paper/scissors were the only possible inputs here
        return "tied";
    }

    switch (playerSelection) {
        case "rock":
            if (computerSelection === "scissors") return "wins";
            if (computerSelection === "paper") return "lost";
            break;

        case "paper":
            if (computerSelection === "rock") return "wins";
            if (computerSelection === "scissors") return "lost";
            break;

        case "scissors":
            if (computerSelection === "paper") return "wins";
            if (computerSelection === "rock") return "lost";
            break;
    }

    // error handling (no match)
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript中的Return语句不会退出该功能? - Return statement in javascript doesn't exit the function? if / Else语句不在函数内部运行 - If/Else statement doesn't run inside function lambda中的s3.getObject第一次运行时不返回任何内容,但在此之后会返回任何内容 - s3.getObject inside lambda doesn't return anything the first time its run but does after that function 的返回语句在 Express FileUpload.mv() 方法中使用后不会被触发 - Return statement of a function doesn't get triggered after using it in Express FileUpload .mv() method 我的JavaScript从一开始就没有在IE中运行,但是在我打开了开发者工具后才运行 - My javascript doesn't run in IE from first instance, but runs after I have Developer Tools opened 我的函数不会在 for 循环中返回对象 - My function doesn't not return object in a for loop 'return false'不会结束我的功能 - 'return false' doesn't end my function 我的函数什么都不返回 - My function doesn't return anything 我的 function 没有返回预期的 output - My function doesn't return the expected output 分配新值的 Function 第一次似乎最初工作,但第二次似乎没有返回到 if 语句的开头 - Function that assigns new value appears to initially work the first time, but doesn't appear to return to beginning of if statement the second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM