In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to write the code of JavaScript recursive function". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to write the code of JavaScript recursive function" can help you solve the problem.
Recursion is a method of solving problems through iteration. In other words, a recursive function is a function that calls itself indefinitely (or until something stops it).
Important knowledge about recursive functions
Whenever you choose to use recursive functions, keep these two basic information in mind.
Message 1: recursion is not IIFE
A recursive function is different from calling a function expression (IIFE) immediately.
IIFE automatically calls itself once.
However, recursive functions automatically call themselves repeatedly for an infinite period of time, or until something stops being called again.
Message 2: a recursive function requires a basic case
The code written to stop the re-invocation of recursive functions is called the basic case.
It's always important to define the basics when creating recursive functions-- so that the functions don't run endlessly, crashing the browser.
Examples of recursive functions
Here is a JavaScript code that returns concatenated countDown () of all values returned through a recursive call to the function.
/ Create a recursive function:function countDown (num) {/ / Define the base case of this recursive function: if (num < 0) {return "Recursion Stopped!";} / / Define the recursive case: return num + "," + countDown (num-1);} / / Invoke the countDown () recursive function:countDown (2); / / The invocation above will return: "2, 1, 0, Recursion Stopped!"
Notes
In the above recursive algorithm, the countDown (num-1) code makes the entire function recursive because it is the code that repeats the countDown () recall itself.
Look at the events behind the scenes.
When we call the countDown function and pass in a value of 2 (that is, countDown (2)), the algorithm starts to run as follows:
Step 1: check whether 2 is less than 0
The computer checks whether the countDown value we passed to the function num parameter is less than 0.
Because 2 is not less than 0, the computer has no code to execute the if statement. Instead, it jumps to the next code after the if statement-- recursive code.
Step 2: execute the return statement
After skipping the if statement, the computer executes the return num + "" + countDown (num-1) code-- but num replaces the parameter with the value (that is, 2) of the parameter, as follows:
Return num + "," + countDown (num-1); return 2 + "," + countDown (2-1); return 2 + "," + countDown (1); step 3: execute recursive statements only
In the code in step 2 above, note that the return command cannot return any value because the return statement contains the recursive code () that countDown (1) calls the countDown function.
Therefore, while keeping the rest of the return statement (that is, 2 + "," +), the computer will only execute recursive code (countDown (1)).
In other words, the countDown (1) code automatically calls this function 1 when countDown is passed into value. The algorithm will then check to see if 1 is less than 0 to restart.
Because 1 is not less than 0, the computer jumps to the recursive code, as follows:
Return 2 + "," + num + "," + countDown (num-1); return 2 + "," + 1 + "," + countDown (1-1); return 2 + "," + 1 + "," + countDown (0); step 4: call recursive code only
Note again that the return command (in step 3) cannot return any value because the return statement contains the recursive code () that countDown (0) calls the countDown function.
Therefore, while keeping the rest of the return statement (that is, 2 + "," + 1 + "," +), the computer will only execute recursive code (countDown (0)). Therefore, the countDown (0) code automatically calls the function 0 when countDown is passed into value.
The function then checks to see if 0 is less than 0 to restart.
Because 0 is not less than 0, the computer jumps to the recursive code, as follows:
Return 2 + "," + 1 + "," + num + "," + countDown (num-1); return 2 + "," + 1 + "," + 0 + "," + countDown (0-1); return 2 + "," + 1 + "," + 0 + "," + countDown (- 1); step 5: execute recursive code only
Again, the return command (in step 4) cannot return any value because the return statement contains a recursive code (countDown (- 1)) to call the countDown function.
Therefore, while keeping the rest of the return statement (that is, 2 + "," + 1 + "," + 0 + "," +), the computer will only execute recursive code (countDown (- 1)). Therefore, the countDown (- 1) code automatically calls the function-1 when countDown is passed into value.
The function will then run again by checking if-1 is less than 0.
At this point,-1 is less than 0. Therefore, the computer will if the code that executes the statement by returning a value, "Recursion Stopped!" As follows:
Return 2 + "," + 1 + "," + 0 + ", +" Recursion Stopped! "
Finally, the return statement now has values that can be effectively connected and returned. Therefore, the return value countDown from will be:
"2, 1, 0, Recursion Stopped!" This is the end of the introduction to "how to write the code for JavaScript recursive functions". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.