如何从第一个功能转到下一个功能?

时间:2017-02-14 13:19:29

标签: javascript function if-statement return

我一直在学习和练习JavaScript,通过阅读书籍和练习我学到的东西。我有点不知道如何让事情发生,并希望你的帮助。

        function guessAge(){
            var userInput = parseInt(prompt("Guess my age!", 100),10);

            if(userInput == 25){
                alert("Correct!");
            }else{
                alert("Incorrect");
            }

            return guessAge();
        }

        function guessMovie(){
            var input = prompt("Guess my favourite movie!");

            if(input == "Lego"){
                alert("Everything is awesome!");
            }else{
                alert("Incorrect!");
            }
        }

        guessAge(); //Initialise guessAge function
        guessMovie(); //Initialise guessMovie function

如果答案错误的话,我想让第一个函数重复,但如果它是正确的话继续下一个函数,但无论是否正确,返回似乎都会继续重复。

2 个答案:

答案 0 :(得分:1)

Rubber duck debugging是查找逻辑错误的好方法。评论中的内容是你在逐行逐步执行代码时大声对橡皮鸭说的话。

function guessAge() {
  /*
    I'm asking the user for input with a default value of 100
    I'm then parsing a string to an integer that is base 10
    I store the result in userInput
  */
  var userInput = parseInt(prompt("Guess my age!", 100), 10);
  /*I check if the user input is 25 */
  if (userInput == 25) {
  /* The user input is 25 so I alert that they are correct */
    alert("Correct!");
  } else {
  /* otherwise I alert that they are incorrect */
    alert("Incorrect");
  }

  /*I return what guessAge returns. Right now there is no other 
    return statements out of the function so I will always call
    this line */
  return guessAge();
}

在第一次通过后再次进行,但这次说出你想要做什么。

function guessAge() {
  /*
    I want the user input to see if they can guess my age
  */
  var userInput = parseInt(prompt("Guess my age!", 100), 10);
  /*I check if the user input is 25 */
  if (userInput == 25) {
  /* The user input is 25 so I alert that they are correct */
    alert("Correct!");
  /* I want to leave the function guessAge after the user guesses
     correctly. */
  } else {
  /* otherwise I alert that they are incorrect */
    alert("Incorrect");
  /* I want to call guessAge again since the user guessed wrong */
  }

  /* this statement is out of my control flow block so it will always
     be reached, even if the user entered the correct age. I want to 
     leave the function when the user enters the correct answer but
     this line will allways call guessAge */
  return guessAge();
}

从那里你可以尝试一些解决方案。请记住,有多种方法可以解决问题。

function guessAge() {
  var userInput = parseInt(prompt("Guess my age!", 100), 10);
  if (userInput == 25) {
    alert("Correct!");
  /* I want to leave the function guessAge after the user guesses
     correctly. if I do nothing after my else we will leave the 
     function without having to do anything */
  } else {
    alert("Incorrect");
  /* I want to call guessAge again since the user guessed wrong */
    guessAge();
  }
} 

请注意,您不必从函数返回任何内容。调用函数时也不必总是返回。如果要将值返回给函数调用者,则使用return。

答案 1 :(得分:0)

您需要在正确猜测后返回。这样你第一种方法就会停止自我调用

function guessAge(){
        var userInput = parseInt(prompt("Guess my age!", 100),10);

        if(userInput == 25){
            alert("Correct!");
            return;
        }else{
            alert("Incorrect");
        }

        return guessAge();
    }

或者,您可以在else块中对该方法进行递归调用。这样,只有在猜测错误时才会调用它。

function guessAge(){
        var userInput = parseInt(prompt("Guess my age!", 100),10);

        if(userInput == 25){
            alert("Correct!");
        }else{
            alert("Incorrect");
            return guessAge();
        }

    }