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 understand recursion in Js

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand recursion in Js". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to understand recursion in Js" together.

recursion basis

Recursive concept

·In the program function directly or indirectly calls itself·Directly calls itself

·Indirectly calling yourself

·Jump out of the structure, there is a jump out to have results

Recursive thinking

·Recursive calls, eventually converted to their own function·If there is a function foo, if it is recursive function, to the final problem or converted to the form of the function foo

The idea of recursion is to transform an unknown problem into a solved problem.

functionfoo(){

... foo(...)...

}

Recursive Steps (Tips)

1. Suppose the recursive function is written

2. Search for recurrence relations

3. Converting the structure of recurrence relation into recursion body

4. Adding Critical Conditions to Recursive Bodies

Simple Recursion Exercise

Sum 1-100

·Analysis:

Suppose that the recursive function has been written as sum, i.e. sum(100), which is the sum of 1-100.

·Find recurrence relations: the relationship between n and n-1, or n-2

sum(n)==sum(n-1)+n

varres=sum(100);

varres=sum(99)+100;

3. Converting Recursive Structures to Recursive Bodies

functionsum(n){

returnsum(n-1)+n;

}

4. Add critical conditions to recursion

·100 converted to 99

99 to 98.

98 to 97.

·...

·Find 2 to find 1

·Find 1 to find 1

sum(1)=1

5. recursive function

functionsum(n){

if(n==1)return1;

returnsum(n-1)+n;

Ask for 1, 3, 5, 7, 9,... The sum of the nth item and the first n items, starting with 0

Suppose the recursive function has completed foo(n), resulting in odd numbers.

·Recursion: ○ foo(n)=foo(n-1)+2

·Recursive body

functionfoo(n){

returnfoo(n)=sum(n-1)+2;

}

4. bailout condition

·foo(n)=foo(n-1)+2

·foo(1)=foo(0)+2

·foo(0)=1;

Thank you for reading, the above is "how to understand recursion in Js" content, after the study of this article, I believe that we have a deeper understanding of how to understand recursion in Js this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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