_** Together, we get better every day. It all adds up, and one day it will. * * _

(a) can not say the JS key knowledge ~ immediately execute the function

(two) can not say the JS key knowledge ~ throttling and anti shake (test little sister topic)

(3) can not say the JS key knowledge ~ recursion and tail recursion (test little sister topic)

(4) can not say the JS key knowledge ~ closure (test little sister topic)

(5) javaScript key knowledge ~~~ singleton pattern (test little sister topic)

We are programmers, and the greatest skill of programmers is to apply programming ideas to life. A few days ago, I talked about life with my sister in the coffee shop, and wanted to give up the current struggles and seek poetry and distance together. Is this her momentary impulse or want to taste the sweetness of love? On a whim, why don’t we reverse test this little sister with recursion? See if she has the stamina. I’m gonna buy him dinner and coffee every day, over and over again.

Recursive meaning:

Function calls itself, when using recursion must have a condition to end recursion, otherwise it will become an endless loop, until the browser crashes.

Recursive conditional chestnut:

The condition that ends the recursion is:

1. If one day she gets impatient and says it’s just an impulse, I’ll stop recursing.

2. If the little sister after every date, each other heart to heart talk about the future, talking about seven aunts, eight aunts, baby topics. Then I’ll show her the house…….

But I still ignore one situation, that is, my little sister wants to spend the rest of her life with me, but she drinks coffee every day except for eating together. Life is wonderful, but eating and drinking coffee takes up most of her energy. This is where the recursion bottleneck comes in. Next, let’s examine the pros and cons of recursion.

Pros and cons of recursion

Advantages:
1. Simple code
2. Easy to understand
For example, in front/middle/back traversal of a tree, recursion is significantly easier to implement than loop.
Disadvantages:
1, time and space consumption is relatively large
Recursion is a function call itself, which consumes time and space. Each function call needs to allocate space in the memory stack to hold parameters, return values and temporary variables, and it takes time to push and pop data onto the stack, thus reducing efficiency.
2. Double counting
In recursion, many calculations are repeated. The essence of recursion is to decompose a problem into two or more small problems. There are overlapping parts of many small problems, that is, there are repeated calculations, such as the recursive realization of Fibonacci sequence.
3. Call stack overflow
Recursion is possible when the call stack overflow, each call will allocate space in the memory stack, and the capacity of stack space is limited, when the number of calls is too many, it may exceed the stack capacity, and then cause the call stack overflow.
Because each time you execute the code, will be allocated a certain size of the stack space (Windows system for 1 m), each method call in the stack store certain information (such as parameters, local variables, return value, etc.), the information be less will also occupy a certain space, accumulated tens of thousands of these Spaces, naturally over the thread stack space. So how to solve such problems?

Solution ~~ tail recursion:

Using factorial as an example, let’s look at tail recursion:
       Tail recursion
Is this:** Returns a function call ** at the last step of function execution

Recursive:

  function factorial(n) {    
        if (n === 1) return 1;      
         return n * factorial(n - 1); 
  }        
  factorial(5) // 120
Copy the code

Tail recursion:

function factorial(n, total) {  
    if (n === 1) return total;    
        return factorial(n - 1, n * total);  
    }    
 factorial(5, 1) // 120
Copy the code
** Theoretically for tail recursion, there is only one call frame, so there is never a “stack overflow” error. However, after testing, tail recursion also shows stack overflow. The results can be seen in the following figure: **

Very pit!! The browser does not implement tail recursion, resulting in savings **! **

Application Cases:

(1) Find the Fibonacci sequence

function getFib(x) {         
       if(x==1||x==2){         
           return 1  
       }         
        return getFib(x-1)+getFib(x-2);    
    }         
console.log(getFib(12));
Copy the code

(2) Find the sum of the digits of a number

getEverySum(x) { if(x<10){ return x; } return x%10+getEverySum(parseInt(x/10));} return x%10+getEverySum(parseInt(x/10)); } console.log(getEverySum(123)); / / 6Copy the code

All right, so is this a spur of the moment test? I have sent wechat to test sister, after work to eat good lunge.

Linjiang Fairy ~ Romance of The Three Kingdoms

As the Yangtze river rolls east,

The waves are all heroes.

Non – success or failure turn head empty.

The green hills are still there,

A few degrees of sunset.

White hair fishing jiang Nagisa,

Used to watching autumn moon and spring breeze.

A pot of liquor happy to meet.

Of all things ancient and modern,

It’s all a joke.