This is the 8th day of my participation in Gwen Challenge

Follow up on the previous one, which focused on higher-order functions (recursive functions)

What is recursion?

  • Recursion is a very important programming skill by which functions call themselves. – MSDN documentation
  • A function calls itself, or calls itself from a function that is a descendant of its own function call.
  • Take, for example, factorial
function factorial(i) {
  if (i === 1) return i;
  return i * factorial(i - 1);
}
console.log(factorial(5)); / / 5 * 4 * 3 * 2 * 1
Copy the code

In several steps

  1. Declare a function factorial that takes a parameter I.

2. Check whether I equals 1. If I equals 1, return I. 3. If I is not equal to 1, return I * factorial(I – 1) and call the function itself again. The function then repeats steps 2-3 until I decreases to 1.

Steps of a recursive function

  1. So let’s say the recursive function is already written
  2. Look for recursive relationships
  3. Transform the structure of a recursive relationship into a recursive body
  4. Add critical conditions to the recursive body

A classic case

  • Sum: Find the sum of 1-100
function sum(n) {
  if (n == 1) return 1
  return sum(n - 1) + n
}
Copy the code
  • Fibonacci ratched sequence: 1,1,2,3,5,8,13,21,34,55,89… O n
// Recursive method
function fib(n) {
  if (n === 1 || n === 2) return n - 1
  return fib(n - 1) + fib(n - 2)}console.log(fib(10)) / / 34
 
// Non-recursive method //
function fib(n) {
  let a = 0
  let b = 1
  let c = a + b
  for (let i = 3; i < n; i++) {
    a = b
    b = c
    c = a + b
  }
  return c
}
console.log(fib(10)) / / 34
Copy the code
  • Climb stairs: if the stairs have n steps, each time you can walk 1 or 2 steps, excuse me, there are several ways to finish these N steps
function climbStairs(n) {
  if (n == 1) return 1
  if (n == 2) return 2
  return climbStairs(n - 1) + climbStairs(n - 2)}Copy the code
  • Clone (o) = new Object; Return an object
function clone(o) {
  var temp = {}
  for (var key in o) {
    if (typeof o[key] == 'object') {
      temp[key] = clone(o[key])
    } else {
      temp[key] = o[key]
    }
  }
  return temp
}
Copy the code
  • Recursive components
    • Recursive components: A component can recursively call itself within its template by setting the name component to the component.
    • Note, however, that you must give a condition to limit the number, otherwise an error will be raised: Max Stack Size exceeded
    • Component recursion is used to develop independent components that have an unknown hierarchy. Examples include cascading selectors and tree controls
<! -- Take VUE as an example --><template>
  <div v-for="(item,index) in treeArr"> {{index}} <br/>
      <tree :item="item.arr" v-if="item.flag"></tree>
  </div>
</template>
<script>
export default {
  // Name must be defined for recursive calls within the component
  name: 'tree'.data(){
    return{}},// Receives the value passed in externally
  props: {
     item: {
      type:Array.default: () = >[]}}}</script>
Copy the code

Recursive functions call methods

  • Recursively called by the function’s own name
function sum(num){
  if(num<=1) {return 1;
  }else{
    return num+sum(num-1); }}console.log(sum(2));/ / 3
Copy the code
  • Call the function itself from arguments.callee
function sum(num){
  if(num<=1) {return 1;
  }else{
    return num+arguments.callee(num-1); }}console.log(sum(3));/ / 6

var sumAnother=sum;
console.log(sumAnother(8));/ / 36

sum=null;
console.log(sumAnother(5));/ / 15
Copy the code
  • The arguments.callee effect is implemented with function named expressions
var sum=(function(){
    'use strict'
    return  function fun(num){
        if(num<=1) {return 1;
        }else{
            return num+fun(num-1); }}}) ()console.log(sum(5));/ / 15

var sumAnother=sum;
console.log(sumAnother(5));/ / 15

sum=null;
console.log(sumAnother(5));/ / 15
Copy the code
  • The above is to introduce the detailed solution of JS recursive function, I hope to help you