简体   繁体   中英

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. 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 . 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. But instead, you are calling playRound() initially in each iteration of your loop, and then again each time you try to determine the outcome.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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