我正在创建剪刀纸摇滚游戏,函数和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和编码还是陌生的,希望这不是一个愚蠢的问题。
请检查下面的代码。...我在功能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>
您需要在随机索引处添加数组,而不是使用数字:
let randomChoice = choice[Math.floor(Math.random()*choice.length)];
而不是:
let randomChoice = [Math.floor(Math.random()*choice.length)];