简体   繁体   English

如何用函数改变变量的值?

[英]How to alter the value of a variable with a function?

I'm building the code for a "Number Guesser Game". 我正在构建“Number Guesser Game”的代码。 How can I change the score after each play? 每场比赛结束后如何更改分数?

It's my first unassisted assignment for an online course. 这是我第一次在线课程的无辅助作业。 I don't have the basic understanding of what to use to achieve certain outcomes so I apologize in advance for the noob question. 我对使用什么来达到某些结果没有基本的了解,所以我提前为noob问题道歉。

I got stuck at task number 5: 我被困在任务5号:

Create an updateScore() function. 创建updateScore()函数。 This function will be used to correctly increase the winner's score after each round. 此功能将用于在每轮后正确增加获胜者的分数。

This function: 这个功能:

Has a single parameter. 有一个参数。 This parameter will be a string value representing the winner. 此参数将是表示获胜者的字符串值。 Increases the score variable (humanScore or computerScore) by 1 depending on the winner passed in to updateScore. 将得分变量(humanScore或computerScore)增加1,具体取决于传递给updateScore的获胜者。 The string passed in will be either 'human' or 'computer'. 传入的字符串将是“人”或“计算机”。 Does not need to return any value. 不需要返回任何值。

First of all: Why do I need to create a function to change the scoreboard in the first place? 首先:为什么我需要创建一个功能来改变记分牌? Couldn't I just write code to change it after I set the winner in the “compareGuesses” function? 在“compareGuesses”函数中设置胜利后,我不能只编写代码来改变它吗?

This is what I have so far: 这是我到目前为止:

// Write your code below: //在下面写下你的代码:

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;


const generateTarget = Math.floor(Math.random() * 9)

const compareGuesses = (humanGuess, computerGuess,targetGuess) => {
  if (Math.abs (humanGuess-targetGuess() )<Math.abs( computerGuess-targetGuess() )){ return true;                                                                        
} else if  (Math.abs (humanGuess-targetGuess() )> Math.abs( computerGuess-targetGuess() )){
  return false; 
}

}


const updateScore = (winner) => {
  if (compareGuesses= true) {
}

First of all: Why do I need to create a function to change the scoreboard in the first place? 首先:为什么我需要创建一个功能来改变记分牌? Couldn't I just write code to change it after I set the winner in the “compareGuesses” function? 在“compareGuesses”函数中设置胜利后,我不能只编写代码来改变它吗?

Yes, you could do that, but to help with code maintainability (and a number of other things), it is often a good idea to split separate chunks or modules of your larger application/problem into smaller functions. 是的,您可以这样做,但为了帮助实现代码可维护性(以及许多其他事情),将较大的应用程序/问题的单独块或模块拆分为较小的函数通常是个好主意。 This idea is known as decomposition . 这种想法被称为分解 Thus, by creating separate functions such as updateScore and compareGuesses you're breaking your bigger problem into smaller more maintainable problems. 因此,通过创建诸如updateScorecompareGuesses类的单独函数,您可以将更大的问题分解为更小的更易维护的问题。


Now for your updateScore function. 现在为您的updateScore功能。 Your description says that it will accept a string, either being "human" or "computer" . 你的描述说它会接受一个字符串,无论是"human"还是"computer" You need to check whether the winner is either "human" or "computer" and update the associated score variable. 您需要检查获胜者是"human"还是"computer"并更新相关的分数变量。 Here is an example on how you can achieve this: 以下是如何实现此目的的示例:

const updateScore = (winner) => {
  if(winner === 'human') {
    humanScore += 1; // humanScore++; will also work here
  } else if(winner === 'computer') {
    computerScore += 1; // computerScore++; will also work here
  }
}

So if you wold like to increase player score if he guess the number correctly and increase computer score if the guess is not correct, the function may look like this: 因此,如果您想要提高玩家得分,如果他正确地猜测数字并且如果猜测不正确则增加计算机得分,则该功能可能如下所示:

function score(humanScore, computerScore)  {
    if (humanGuess - computerGuess === 0) {
        humanScore += 1
  } 
    else {
        computerScore += 1
   } 
alert(humanScore) 
alert(computerScore) 
}

Of course, you need to call a function like this: 当然,你需要调用这样的函数:

score(humanScore, computerScore)

Try something like this: 尝试这样的事情:


class Game {
  humanScore = 0;
  computerScore = 0;
  currentRoundNumber = 1;

  target = 0;

  generateTarget() {
    this.target = Math.floor(Math.random() * 9)
  }

  nextRound(humanGuess, computerGuess) {
    this.currentRoundNumber++;

    if (Math.abs(humanGuess-this.target) < Math.abs(computerGuess-this.target))
      this.humanScore++;                                                  
    else if (Math.abs(humanGuess-this.target) > Math.abs(computerGuess-this.target))
      this.computerScore++;
  }
}

const game = new Game();
game.generateTarget();
game.nextRound(333, 42);

PS: StackOverflow is not here to do your homework. PS:StackOverflow不在这里做你的功课。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM