简体   繁体   English

在另一个函数中调用一个函数

[英]Calling a function inside another function

I am trying to call the getWinner function inside the playFiveTimes function and every time I try to test my code it gives me an error that says: "playerMove undefined" or if I try and type "rock" it says that is undefined as well. 我试图在playFiveTimes函数中调用getWinner函数,并且每次尝试测试代码时,都会给我一个错误,提示:“ playerMove undefined”,或者如果我尝试键入“ rock”,它也表示未定义。 I am completely lost at this point. 在这一点上我完全迷失了。 Any help would be appreciated. 任何帮助,将不胜感激。

'use strict';

function getInput() {
  console.log("Please choose either 'rock', 'paper', or 'scissors'.")
  return prompt();
}

function randomPlay() {
  var randomNumber = Math.random();
  if (randomNumber < 0.33) {
    return "rock";
  } else if (randomNumber < 0.66) {
    return "paper";
  } else {
    return "scissors";
  }
}

function getPlayerMove(move) {
  if (!move) {
    move = getInput();
  }
  return move;
}

function getComputerMove(move) {
  if (!move) {
    move = getInput();
  }
  return move;
}

function getWinner(playerMove, computerMove) {
  var winner;
  if (playerMove === computerMove) {
    return ("It's a tie!");
  } else if (playerMove === "rock") {
    if (computerMove === "paper") {
      return "paper wins";
    } else {
      return "rock wins";
    }
  } else if (playerMove === "paper") {
    if (computerMove === "rock") {
      return "paper wins";
    } else {
      return "scissors wins";
    }
  } else if (playerMove === "scissors") {
    if (computerMove === "paper") {
      return "scissors wins";
    } else {
      return "rock wins";
    }
  }
  return winner;
}

function playToFive() {
  console.log("Let's play Rock, Paper, Scissors");
  var playerWins = 0;
  var computerWins = 0;
  // Write code that plays 'Rock, Paper, Scissors' until either the player or the computer has won five times.
  while ((playerWins < 5) && (computerWins < 5)) {
    getWinner();
    if (playerWins) {
      playerWins += 1
    } else {
      computerWins += 1
    }
  }

  console.log("Player chose " + playerMove + " while computer chose " + computerMove);
  console.log("The score is currently " + playerWins + " to " + computerWins + "\n");
  return [playerWins, computerWins];
}
console.log(playToFive());

You are calling the function with no parameters. 您正在调用没有参数的函数。 It expects two parameters, as you specified, function getWinner(playerMove, computerMove) {...} . 如您所指定的,它需要两个参数, function getWinner(playerMove, computerMove) {...} So, supply the two parameters in the function call, ie getWinner('rock', 'paper') . 因此,在函数调用中提供两个参数,即getWinner('rock', 'paper') The program only does what you tell it. 该程序只会执行您所说的操作。 You cannot call getWinner() with no parameters and expect a result. 不能不带参数调用getWinner()并期望结果。

Second, there is no use for the variable winner inside the same function. 其次,在同一函数中没有变量winner You literally do not use it at all except to return it if there is no winner. 实际上,您根本不使用它,除非没有获胜者将其退还。 That's counterintuitive. 这是违反直觉的。

Third, you never define playerMove and computerMove in the playToFive() function, and you never assign them a value. 第三,您永远不要在playToFive()函数中定义playerMovecomputerMove ,也永远不要给它们分配值。

Debugging and desk-checking your code helps lead you to these facts pretty easily. 调试和检查代码有助于您轻松地找到这些事实。

Your script is crashing on this line: 您的脚本在此行崩溃:

console.log("Player chose " + playerMove + " while computer chose " + computerMove);

inside your playToFive() function because playerMove and computerMove variables are undefined but you're trying to cast it to string. 在playToFive()函数中,因为playerMovecomputerMove变量未定义,但是您试图将其playerMove为字符串。 You need to define your variables before using it. 您需要先定义变量,然后再使用它。

This is the reason for the "playerMove undefined" error but also you should check your program's logic as it was said before by Brett and Ivern 这是“ playerMove undefined”错误的原因,但是您也应该像Brett和Ivern之前所说的那样检查程序的逻辑

I thing that the BIGGEST mistake is doing function inside function, try: 我认为最大错误是在函数内部执行函数,请尝试:

if (playerMove === "rock") | 如果(playerMove ===“ rock”)| (computerMove === "paper") { return "paper wins"; (computerMove ===“ paper”){返回“ paper wins”; } }

I thing that might work. 我的事情可能会起作用。 The "|" “ |” means and, you probably know that, but just I am not sure 意思是,你可能知道,但是我不确定

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

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