【发布时间】:2021-09-04 23:41:31
【问题描述】:
这个问题与The Odin Project Rock Paper Scissors JavaScript Fundamentals course直接相关。
我已经成功地制作了一个为计算机随机挑选“石头”、“纸”或“剪刀”的功能,称为“computerPlay”。
function computerPlay() {
const gameArray = ['Rock', 'Paper', 'Scissors'];
const random = Math.floor(Math.random() * gameArray.length);
return gameArray[random];
}
我制作了另一个函数来接受玩家输入的“rock”、“paper”、“scissors”(或分别为 1、2、3),在另一个名为“playerPlay”的函数中不考虑大小写。
function playerPlay() {
console.log ("Do you choose 'Rock' (1), 'Paper' (2), or 'Scissors' (3)?");
let playerChoice = prompt("Do you choose 'Rock' (1), 'Paper' (2), or 'Scissors' (3)?");
if (playerChoice.toLowerCase() === "rock" || parseInt(playerChoice) === 1) {
playerChoice = "Rock";
} else if (playerChoice.toLowerCase() === "paper" || parseInt(playerChoice) === 2) {
playerChoice = "Paper";
}
else if (playerChoice.toLowerCase() === "scissors" || parseInt(playerChoice) === 3) {
playerChoice = "Scissors";
}
else if (playerChoice.toLowerCase() !== "rock" || "scissors" || "paper" || parseInt(playerChoice) !== 1 || 2 || 3) {
alert("Please try to enter your value again :)")
playerSelection();
}
else {
console.log("Not sure what's going on, hold on to yer butts ;)")
}
return playerChoice;
}
函数playerPlay()保存到const变量playerSelection,函数computerPlay()保存到const变量computerSelection。
// Create a variable to store player's choice
const playerSelection = playerPlay();
// Create a variable to store computer's choice
const computerSelection = computerPlay();
然后在 playRound() 函数中使用这两个 const 变量,该函数运行一组比较以查看然后 console.log 并返回玩家或计算机是否赢得了石头、纸和剪刀的回合。
/* Write a function that plays a single round of Rock Paper Scissors. The function should take two parameters - the playerSelection and
computerSelection - and then return a string that declares the winner of the round like so: "You Lose! Paper beats Rock" */
function playRound(playerSelection, computerSelection) {
if (computerSelection.toLowerCase() === playerSelection.toLowerCase()) {
console.log("It's a draw!");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "rock" && playerSelection.toLowerCase() === "scissors") {
console.log("Rock beats scissors! You lose :(");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "scissors" && playerSelection.toLowerCase() === "rock") {
console.log("Rock beats scissors! You win :D");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "scissors" && playerSelection.toLowerCase() === "paper") {
console.log("Scissors beats paper! You lose :(");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "paper" && playerSelection.toLowerCase() === "scissors") {
console.log("Scissors beats paper! You win :D");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "paper" && playerSelection.toLowerCase() === "rock") {
console.log("Paper beats rock! You lose :(");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else if (computerSelection.toLowerCase() === "rock" && playerSelection.toLowerCase() === "paper") {
console.log("Paper beats rock! You win :D");
return("Your Choice: " + playerSelection + ". " + "Computer: " + computerSelection);
} else {
console.log("I'm not sure what, but something went worng :(");
}
}
我的问题来自于我需要重复 playRound() 函数 5 次,并且每一轮都不同。
/* Write a NEW function called game(). Use the previous function inside of this one to play a 5 round game that keeps score and reports
a winner or loser at the end. */
// Define function / rule for winning and keeping track of score and determining winner for 5 rounds of game
// Make each round reset the computer choice and get new input from the player.
function game() {
roundOne;
roundTwo;
roundThree;
roundFour;
roundFive;
}
const roundOne = playRound(playerSelection, computerSelection);
const roundTwo = playRound(playerSelection, computerSelection);
const roundThree = playRound(playerSelection, computerSelection);
const roundFour = playRound(playerSelection, computerSelection);
const roundFive = playRound(playerSelection, computerSelection);
// Print results of game round
console.log(game(playerSelection, computerSelection));
我最近尝试创建 5 个新的 const 变量(roundOne、roundTwo...roundFive)来包含“playRound(playRound(playerSelection,computerSelection);”功能。我还尝试将 playerSelection 和 computerSelection const 变量更改为 let,认为 const 的性质可能会将值锁定到位,直到我重新加载页面。我认为这可能是一个范围问题,但我不明白它到底在哪里——这就是我想在这里问的原因。主要问题是玩家输入的值和计算机随机生成的值在 5 次重复回合中始终相同。如何为每个重复的回合打印不同的值?这可能与我如何使用“gameArray”(在 computerPlay() 函数中)和“playerChoice”(在 playerPlay() 函数中)有关吗?
如果这比需要的时间长得多,我深表歉意。我也会继续调查。非常感谢您。
(这是我第一个实际的 JavaScript 创建/任何类型的项目,所以任何一般的反馈,或者我如何发布/提出这个问题,也将不胜感激。谢谢!)
【问题讨论】:
标签: javascript function