Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is JavaScript Recursion and how to use it

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is JavaScript recursion and how to use it". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what JavaScript recursion is and how to use it.

What is recursion and how does it work?

Let's first look at the definition of recursion:

Recursion is an effective way to solve the problem. In the process of recursion, the function calls itself as a subroutine.

To put it simply, the programming skill in which a program calls itself is called recursion. The idea of recursion is to transform a large complex problem into a problem with a smaller scale than the original problem. After the problem is disassembled into sub-problems, the recursive call continues until the sub-problem can be solved without further recursion.

Using recursion needs to avoid endless loops, and to ensure that recursion works correctly, the recursive program should contain two attributes:

Basic situation (bottom cases), the basic situation is used to ensure that the program call returns in time, does not continue recursion, and ensures that the program can be terminated.

Recursive relationship (recurrentce relation), which splits all other cases into base cases.

Calling yourself in the function is recursive. Remember that there must be termination conditions, otherwise you will enter an endless loop.

1. Summation (1) Digital Sum function sum (n) {if (n) {return naugh1} return n+sum (NMel 1)} console.log (sum (5))

Execution sequence

5+sum (nmur1)

5+4+sum (nmur1)

5+4+3+sum (nmur1)

5+4+3+2sum (nmur1)

Five, four, three, two, one.

(2) array summation function sum (arr) {var len = arr.length if (len = = 0) {return 0} else if (len = = 1) {return arr [0]} else {return arr [0] + sum (arr.slice (1))} let arr = [1,2,3,4,5] console.log (sum (arr))

Execution sequence

1+sum (2, 3, 4, 5)

1+2+sum (3, 4, 5)

1, 2, 3 (4, 5)

1+2+3+4+sum (5)

1 / 2 / 3 / 4 / 5

One, two, three, nine.

1: 2: 12

1: 14

fifteen

2. Data transfer let data = [{id: "01", pid: "," name ":" Lao Wang "}, {id:" 02 ", pid:" 01 " "name": "Xiao Zhang"}] function fn (data RootId = "") {const children = [] / / define an empty array data.forEach (item = > {if (item.pid = rootId) {/ / if the pid=rootId of each item is added to the array children.push (item) item.children = fn (data) Item.id)}}) Return children}

To use a recursive tree, you need to know what the pid value of the root is before you can take the next step as a starting point.

Execution sequence

III. Hanoi Tower

Under the rule, the three columns are set to a, b, c respectively, and the goal is to put all the plates in an into the c column from big to small, and only one plate can be moved at a time.

The idea of realization is:

Move 1 plate from a to b

Move the n plate from a to c

Move 1 plate from b to c

Function fn (num, start, middle, end) {if (num > 0) {fn (num-1,start,end,middle) console.log (start+ "= >" + end); fn (num-1,middle,start,end)} fn (2, "a", "b", "c")

Take num as the number of plates start as a plate middle as b plate end as c plate

For example, the execution order of two plates

1. The first line takes 2 into num > 0 to execute the first function fn (2-1 start) and then executes fn (1-1 start) finds that num is less than 0 and not only the if condition, but also looks back at fn (2-1 start) and outputs console.log (console.log = > b)

two。 The second line console.log (start+ "= >" + end); output directly as follows = > c

3. The third line fn (2-1 fn) executes console.log (bounded attention = > c) next time you go to execute fn (1-1 menagerie start), you can't finish the cycle.

The order of execution is a little abstract, so if we don't understand it, we will do fn (num, start, middle, end) according to the simplest way of thinking. We usually do what we do when we play games.

Fn (num-1,start,middle,end) takes the parameter position of the second as the plate to be moved, and the parameter position of the fourth as the moving target and brings this formula in every time he looks at the picture.

The first step, fn (num-1,start,end,middle), for example, two plates, must put Amurl on b first.

The second step is to put plate an on c and directly output console.log ("a plate = > c").

Step 3: put plate b on c and fn (num-1,middle,start,end,)

IV. Fibonacci series

Fibonacci series (Fibonacci sequence), also known as golden section series, is also called "rabbit series" because mathematician Leonardo Fibonacci (Leonardoda Fibonacci) introduced rabbit breeding as an example, which refers to such a series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Function Fibonacci (n) {return n

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report