pav
6/17/2013 - 5:19 PM

recursive_guessing_game

function guessNumber(number, clue) {
  // Prompt the user for a number using the string value of clue
  var guess = prompt(clue);  
  // 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 first recursive case (guess is too low)
  else if (guess < number) {
    return guessNumber(number, "Too low guess again"); 
  }
  // Define second recursive case (guess is too high)
  else if (guess > number) {
      return guessNumber(number, "Too high guess again");
    
  }
}

guessNumber(3, "Guess a number between 1 and 3.");