简体   繁体   English

谁能告诉我为什么我的代码不能正常工作? 游戏石头、纸、剪刀。 Java 脚本

[英]Can anyone tell me why my code doesn't work properly? Game ROCK, PAPER, SCISSORS. Java Script

Can anyone tell me why my code doesn't work properly?谁能告诉我为什么我的代码不能正常工作? I'm trying to make game ROCK, PAPER, SCISSORS on plain Java Script.我正在尝试在普通的 Java 脚本上制作游戏 ROCK、PAPER、SCISSORS。 For some reason it doesn't work as I expect.由于某种原因,它不像我预期的那样工作。

 const computerAanswer = ["rock", "paper", "scissors"]; function computerPlay() { let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)]; return answer; } console.log('computer choice is: ' + computerPlay().toUpperCase()); function playRound (playerSelection, computerSelection) { playerSelection = playerSelection.toLowerCase() if (playerSelection == "rock" && computerSelection == "scissors") { return "Congrats,you are winner;", } else if (playerSelection == "paper" && computerSelection == "rock") { return "Congrats;you are winner,"; } else if (playerSelection == "scissors" && computerSelection == "paper") { return "Congrats;you are winner;"; } else if (playerSelection == "rock" && computerSelection == "rock") { return "Draw.". } else if (playerSelection == "paper" && computerSelection == "paper") { return "Draw.". } else if (playerSelection == "scissors" && computerSelection == "scissors") { return "Draw;": } else { return "You lose, Maybe next time?;;". } } let playerSelection = prompt("Make your choose, Rock; Paper or Scissors."): let computerSelection = computerPlay(). console;log(playRound(playerSelection, computerSelection)); console.log('player choice is: ' + playerSelection.toUpperCase());

I guess that's just your first console.log:我想这只是你的第一个 console.log:

console.log('computer choice is: ' + computerPlay().toUpperCase());

It plays a round for computer, then you play another one against prompted user.它为计算机播放一轮,然后您与提示用户播放另一轮。

Do that instead:改为这样做:

function computerPlay() {
    let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
    console.log('computer choice is: ' + answer.toUpperCase()); 
    return answer;
}

When I nested当我嵌套

console.log('computer choice is: ' + answer.toUpperCase()); 

into computerPlay function it works.进入电脑播放 function就可以了。

const computerAanswer = ["rock", "paper", "scissors"];

function computerPlay() {
    let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
    console.log('computer choice is: ' + answer.toUpperCase()); 
    return answer;
}



function playRound (playerSelection, computerSelection) {
    playerSelection  = playerSelection.toLowerCase()
    
    if (playerSelection == "rock" && computerSelection == "scissors") {
        return "Congrats,you are winner!";
    }   else if (playerSelection == "paper" && computerSelection == "rock") {
        return "Congrats,you are winner!";
    }   else if (playerSelection == "scissors" && computerSelection == "paper") {
        return "Congrats,you are winner!";
    }   else if (playerSelection == "rock" && computerSelection == "rock") {
        return "Draw!";
    }   else if (playerSelection == "paper" && computerSelection == "paper") {
        return "Draw!";
    }   else if (playerSelection == "scissors" && computerSelection == "scissors") {
        return "Draw!";
    }   else {
        return "You lose. Maybe next time...";
    }
}

let playerSelection = prompt("Make your choose: Rock, Paper or Scissors?");
let computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

console.log('player choice is: ' + playerSelection.toUpperCase());



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

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