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

How to implement Recursion in JavaScript

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces JavaScript how to achieve recursion, the article is very detailed, has a certain reference value, interested friends must read it!

What is recursion?

If a function can call itself internally, then the function is a recursive function. Simple understanding: the function calls itself inside, and this function is a recursive function.

As follows:

Function fn () {fn ();} fn ()

This function is a recursive function, and when we print it directly, it will:

Why did you find a print error? Because recursive functions have the same effect as loops. When no value is returned to him, it will go on and on. So, we know:

Because recursion is prone to stack overflow errors (stack overflow), an exit condition return must be added.

How should the correct recursive function be written? Take the above code as an example: for example, if we are going to print "Hello" five times, we should write it like this:

Var num = 1 * function fn () {console.log ('Hello'); if (num = = 5) {return;} num++; fn ();} fn ()

The print result is as follows:

Now that we know what recursion is, let's take a look at how to use recursion to solve problems.

Second, using recursion to solve mathematical problems

1. Ask for 1 * 2 * 3 * 4. Factorial of * n

The code is as follows:

Function fn (n) {if (n = 1) {return 1;} return n*fn (nMel 1);} console.log (factorial of'1-20 is:'+ fn (20)); console.log (factorial of'1-10 is:'+ fn (10)); console.log (factorial of'1-5 is:'+ fn (5))

The print result is as follows:

2. Find the Fibonacci series

Fibonacci series, also known as "rabbit series", refers to such a series:, 1, 1, 2, 3, 5, 8, 13, 21, 34, …... That is, the value of the third item is the sum of the first two. The user enters an n to get the number of that position.

The code is as follows:

Function fb (n) {if (n = 1 | | n = 2) {return 1;} return fb (nmel1) + fb (nmel2);} console.log ('3rd Fibonacci series:' + fb (3)); console.log ('10th Fibonacci series:' + fb (10))

The print result is as follows:

Third, using recursion to find the corresponding data object

Return the corresponding data object according to id

There are objects like the following:

Var date = [{id:1, name:' Appliance', goods: [{id: 11, gname:' Mobile'}, {id: 12, gname:' computer'}]}, {id:2 Name:' clothing', goods: [{id: 21, gname:' trousers'}, {id: 22, gname:' jacket'}]}, {id: 3 Name: 'food'}]

Now you want to return the corresponding data object by typing id.

First, we can iterate through the array with for...Each () to get each value, as shown below:

Function getId (array,id) {array.forEach (function (value) {console.log (value);})} getId (date,1)

The printed result is:

At this point, if we want to get the value of an object with an id of 1, we can do this:

Function getId (array,id) {array.forEach (function (value) {if (value.id = id) {console.log (value);}})} getId (date,1)

The print result is as follows:

You can get it, but what if we want to get the value of an object with id 11? Obviously, it is not feasible to call this function directly, because we only get the value of the outermost object through for...Each, but the specific classification of the inner layer is not obtained, so we can get the value of the inner object by calling the getId (array,id) function recursively.

Do the following:

Function getId (array,id) {array.forEach (function (value) {if (value.id = id) {console.log (value);} else if (value.goods & & value.goods.length! = 0) {getId (value.goods,id) }})} / / getId (date,1); getId (date,11)

The printed result is:

The above is all the contents of the article "how to achieve recursion in JavaScript". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report