电源功能 - 分配索引

时间:2016-07-30 23:00:22

标签: javascript recursion

工作"更高,更低"在codeacademy中的功能。 有人可以解释为什么我们使用相同的参数来调用guessMyNumber函数内的相同函数?

我的印象是我需要减少或增加参数。

请参阅以下代码。



function guessNumber(number) {
  // Prompt the user for a number
  guess = prompt("Guess a number between 1 and 100");

  // Convert their guess to a number using +
  guess = +guess;

  // Define base case
  if (guess == number) {
    return console.log("You got it! The number was " + number);
  }
  // Define recursive case with a function call
  if (guess < number) {
    console.log("Higher");
    guessNumber(number);
  } else {
   console.log("Lower");
   guessNumber(number); 
  }
}

// Call the function guessNumber() with an integer for an argument
guessNumber(5);
&#13;
&#13;
&#13;

0 个答案:

没有答案