简体   繁体   English

JavaScript:石头剪刀布。 Function 错误

[英]JavaScript: rock paper scissors. Function error

im trying to create the game "rock, paper or scissors" in JavaScript, but im stuck on the last function "game()", it should repeat 5 times the function playRound() and throw a result each of those 5 times.我试图在Z686155AF75A5A60A0F6E9D80C1F7EDD3EE9Z中创建游戏“摇滚,纸或剪刀”,但我坚持在最后一个ZC1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C1C18385D1AB5074C174C17A94F14F14F14Z“ GAME)中,它应该重复5次。 But doesnt work.但不起作用。

function computerPlay(){
    let optionsList = ["rock", "paper", "scissors"];
    let option = optionsList[Math.floor(Math.random() * optionsList.length)];
    return option
}

function playRound(playerSelection, computerSelection){
    var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase();
    var computerSelection = computerPlay();

    if (playerSelection = "rock"){
        switch(computerSelection){
            case "rock":
                return "T";
            case "paper":
                return "M";
            case "scissors":
                return "Y";
        }
    }else if (playerSelection = "paper"){
        switch(computerSelection){
            case "rock":
                return "Y";
            case "paper":
                return "T";
            case "scissors":
                return "M";
        }
    }else if (playerSelection = "scissors"){
        switch(computerSelection){
            case "rock":
                return "M";
            case "paper":
                return "Y";
            case "scissors":
                return "T";
        }
    }
}

function game(){
    let i = 0
    for(i=1; i<=5; i++){
        playRound()
        if (playRound()= "M") {
            console.log("Machine Wins");
        }else if (playRound() = "Y"){
            console.log("You Win")
        }else if (playRound() = "T"){
            console.log("You Win!")
        }
    }
}

Not sure it this fixes the problem but here's a syntax error: = instead of ==.不确定这是否解决了问题,但这里有一个语法错误:= 而不是 ==。 Also, you shouldn't call playRound() in every else .此外,您不应在else中调用playRound() Just store the result in a variable.只需将结果存储在变量中。

 let result = playRound()
 if (result== "M") {                              
        console.log("Machine Wins");
 }else if (result == "Y"){
        console.log("You Win")             
 }else if (result == "T"){
        console.log("You Win!")
 }

You have a couple errors:你有几个错误:

  • The first is that you are trying to use the = operator to compare values.首先是您尝试使用=运算符来比较值。 You need to use == or === .您需要使用=====
  • Secondly, you need to get the return value of playRound() and then check its value to determine the outcome of the game.其次,您需要获取playRound()的返回值,然后检查其值以确定游戏的结果。 But instead, you are calling playRound() initially in each iteration of your loop, and then again each time you try to determine the outcome.但是,您最初是在循环的每次迭代中调用playRound() ,然后在每次尝试确定结果时再次调用。

Try:尝试:

 function computerPlay() { let optionsList = ["rock", "paper", "scissors"]; let option = optionsList[Math.floor(Math.random() * optionsList.length)]; return option } function playRound(playerSelection, computerSelection) { var playerSelection = prompt("Choose: rock, paper or scissors").toLowerCase(); var computerSelection = computerPlay(); if (playerSelection === "rock") { switch (computerSelection) { case "rock": return "T"; case "paper": return "M"; case "scissors": return "Y"; } } else if (playerSelection === "paper") { switch (computerSelection) { case "rock": return "Y"; case "paper": return "T"; case "scissors": return "M"; } } else if (playerSelection === "scissors") { switch (computerSelection) { case "rock": return "M"; case "paper": return "Y"; case "scissors": return "T"; } } } function game() { let i = 0 for (i = 1; i <= 5; i++) { let outcome = playRound() if (outcome === "M") { console.log("Machine Wins"); } else if (outcome === "Y") { console.log("You Win") } else if (outcome === "T") { console.log("You Win;") } } } game();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM