每次使用同一个JavaScript函数时,如何从中获取新值



这个问题与Odin Project Rock Paper Scissors JavaScript基础课程直接相关。

我已经成功地制作了一个函数;岩石"纸,";或";剪刀";对于称为";computerPlay">

function computerPlay() {
const gameArray = ['Rock', 'Paper', 'Scissors'];
const random = Math.floor(Math.random() * gameArray.length);
return gameArray[random];
}

我制作了另一个功能来接受玩家输入的";岩石"纸,"剪刀";(或分别为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((被保存到常量变量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((函数中使用这两个常量变量,该函数运行一组比较,以查看然后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个新的常量变量(roundOne,roundTwo…roundFive(来包含";playRound(playerSelection,computerSelection("作用我还尝试将playerSelection和computerSelection常量变量更改为let,认为常量性质可能会将值锁定在适当的位置,直到我重新加载页面。我认为这可能是一个范围问题,但我不明白它到底在哪里——这就是我想在这里问的原因。主要问题是,玩家输入的值和计算机随机生成的值在5轮重复中始终相同。如何为每个重复的回合打印不同的值?这可能与我如何使用";gameArray";(在computerPlay((函数中(和";playerChoice;(在playerPlay((函数中(?

如果这比需要的时间长得多,我很抱歉。我也会继续调查。事先非常感谢。

(这是我第一个实际的JavaScript创建/任何类型的项目,所以任何一般的反馈,或者我如何发布/问这个问题,也将不胜感激。谢谢!(

据我所知,您只设置了一次常量playerSelectioncomputerSelection(它们是常量,因此无法修改(。然后将它们传递到playRound函数中,该函数将处理相同的常量5次。考虑完全删除这些常量,并像这样编写游戏函数:

function game() {
//Play game 5 times
for (let i = 0; i < 5; i++) {
const playerSelection = playerPlay();
const computerSelection = computerPlay();
// Call playRound function, passing in newly returned values
// from the playerPlay and computerPlay functions
const currentRound = playRound(playerSelection, computerSelection);
// Log our result
console.log(currentRound);
}
}

或者,更简洁地说:

function game() {
//Play game 5 times
for (let i = 0; i < 5; i++) {
// Call playRound function, passing in newly returned values
// from the playerPlay and computerPlay functions and log to console
console.log(playRound(playerPlay(), computerPlay()));
}
}

最新更新