我正在创建一个Scissors Paper摇滚游戏, 函数和if语句。我现在遇到一个仅返回DRAW情况的错误。 如果有人发现该错误,将不胜感激。
我的代码的链接在这里: https://github.com/BladeMountain/SissorPaperRock/blob/master/SPR_script.html
function computerPlay() {
let choice = ['rock', 'paper', 'scissor'];
let randomChoice = [Math.floor(Math.random() * choice.length)];
return randomChoice;
}
function playGame(playerSelection, computerSelection) {
if (playerSelection == "scissor" && computerSelection == "paper") {
return "You Won";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "You Won";
} else if (playerSelection == "rock" && computerSelection == "scissor") {
return "You Won";
} else if (playerSelection == "scissor" && computerSelection == "rock") {
return "You Lost";
} else if (playerSelection == "paper" && computerSelection == "scissor") {
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "paper") {
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "rock") {
return "Draw";
} else if (playerSelection == "scissor" && computerSelection == "scissor") {
return "Draw";
} else if (playerSelection == "paper" && computerSelection == "paper") {
return "Draw";
} else {
return 'Invalid response, this is case insensitive. Please input either scissor, paper or rock';
}
}
function game() {
let roundOne = playGame(playerSelection, computerSelection);
let roundTwo = playGame(playerSelection, computerSelection);
let roundThree = playGame(playerSelection, computerSelection);
let roundFour = playGame(playerSelection, computerSelection);
let roundFive = playGame(playerSelection, computerSelection);
console.log('Round 1 = ' + roundOne);
console.log('Round 2 = ' + roundTwo);
console.log('Round 3 = ' + roundThree);
console.log('Round 4 = ' + roundFour);
console.log('Round 5 = ' + roundFive);
}
let playerSelection = prompt("Please select one", "scissor/paper/rock");
let computerSelection = computerPlay();
console.log(playGame(playerSelection, computerSelection));
// this is only still executing my else statement. Anything that I need to change?
另外,我是StackOverflow和编码的新手,希望这不是一个愚蠢的问题。
答案 0 :(得分:-1)
请检查下面的代码。...我对函数computerPlay()进行了更改
不是返回随机数,而是返回数组值...只是返回函数computerPlay()的一个更改
// this is only still executing my else statement. Anything that I need to change?
function computerPlay() {
let choice = ['rock', 'paper', 'scissor'];
let randomChoice = [Math.floor(Math.random()*choice.length)];
return choice[randomChoice];
}
function playGame(playerSelection, computerSelection) {
if (playerSelection == "scissor" && computerSelection == "paper"){
return "You Won";
} else if (playerSelection == "paper" && computerSelection == "rock"){
return "You Won";
} else if (playerSelection == "rock" && computerSelection == "scissor"){
return "You Won";
} else if (playerSelection == "scissor" && computerSelection == "rock"){
return "You Lost";
} else if (playerSelection == "paper" && computerSelection == "scissor"){
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "paper"){
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "rock"){
return "Draw";
} else if (playerSelection == "scissor" && computerSelection == "scissor"){
return "Draw";
} else if (playerSelection == "paper" && computerSelection == "paper"){
return "Draw";
} else {
return'Invalid response, this is case insensitive. Please input either scissor, paper or rock';
}
}
function game() {
let roundOne = playGame(playerSelection, computerSelection);
let roundTwo = playGame(playerSelection, computerSelection);
let roundThree = playGame(playerSelection, computerSelection);
let roundFour = playGame(playerSelection, computerSelection);
let roundFive = playGame(playerSelection, computerSelection);
console.log('Round 1 = ' + roundOne);
console.log('Round 2 = ' + roundTwo);
console.log('Round 3 = ' + roundThree);
console.log('Round 4 = ' + roundFour);
console.log('Round 5 = ' + roundFive);
}
let playerSelection = prompt("Please select one", "scissor/paper/rock");
let computerSelection = computerPlay();
console.log(playGame(playerSelection, computerSelection));
// this is only still executing my else statement. Anything that I need to change?
<!DOC HTMl>
<html>
</html>