【问题标题】:How do I get a new value from the same JavaScript function each time I use it?每次使用同一个 JavaScript 函数时,如何获得一个新值?
【发布时间】: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


【解决方案1】:

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

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()));
   }
}

【讨论】:

  • 非常感谢! +1 确认:playerSelectioncomputerSelection 被定义为 const 变量是全局范围问题吗?因此,通过在game() 函数的循环中定义playerSelectioncomputerSelection,它使得每次由于内部范围而返回一个新值? (即 playerSelectioncomputerSelection 仅在循环中具有值,因此每次调用 playerPlay()computerPlay() 函数时都会更改,因为它们是全局范围的?)
  • 是的,你是对的。您在全局范围内定义了 playerSelectioncomputerSelection,而且只定义了一次。因此,每当您在代码中的其他任何地方引用它们时,您都在使用它们的唯一实例。我在循环中定义了它们,因此(正如您在评论中正确说明的那样),它们被定义为仅在循环的单次迭代中使用,然后被销毁。然后在我的第二个示例中,我什至根本没有费心将函数的结果分配给变量,因为将它们保存在内存中以便稍后在时钟滴答声中销毁它们并没有多大意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多