当前位置:网站首页>That's why you can't understand recursion
That's why you can't understand recursion
2022-07-06 22:23:00 【Eat by technology】
Today, let's talk about recursion , Bloggers have stumbled here , Don't be like a blogger !!
Two 、 Use recursion to find 1~n The factorial
3、 ... and 、 Using recursion to find Fibonacci sequence
Four 、 Using recursion to traverse data
One 、 What is recursion
recursive : If a function can call itself internally , So this function is recursive . Simple understanding : The function itself calls itself , This function is a recursive function .
Be careful : Recursive functions work just as well as loops , Because recursion is easy to happen “ Stack overflow ” error (stack overflow), So you have to add exit conditions return.
Two 、 Use recursion to find 1~n The factorial
// Use recursive function to find 1~n The factorial 1 * 2 * 3 * 4 * ..n
function fn(n) {
if (n == 1) { // The end condition
return 1;
}
return n * fn(n - 1);
}
console.log(fn(3));The result of the above code is :

3、 ... and 、 Using recursion to find Fibonacci sequence
// Using recursive function to find Fibonacci sequence ( Rabbit sequence ) 1、1、2、3、5、8、13、21...
// The user enters a number n We can work out This number corresponds to the rabbit sequence value
// We just need to know what the user entered n The first two items (n-1 n-2) You can figure it out n Corresponding sequence value
function fb(n) {
if (n === 1 || n === 2) {
return 1;
}
return fb(n - 1) + fb(n - 2);
}
console.log(fb(6));The result of the above code is :
![]()
Four 、 Using recursion to traverse data
// We want to make input id Number , Data objects that can be returned
var data = [{
id: 1,
name: ' Home appliance ',
goods: [{
id: 11,
gname: ' The refrigerator ',
goods: [{
id: 111,
gname: ' haier '
}, {
id: 112,
gname: ' beauty '
},
]
}, {
id: 12,
gname: ' Washing machine '
}]
}, {
id: 2,
name: ' dress '
}];
//1. utilize forEach Go through every object inside
function getID(json, id) {
var o = {};
json.forEach(function(item) {
// console.log(item); // 2 Array elements
if (item.id == id) {
// console.log(item);
o = item;
return o;
// 2. We want to get the data from the inner layer 11 12 You can use recursive functions
// There should be goods This array, and the length of the array is not 0
} else if (item.goods && item.goods.length > 0) {
o = getID(item.goods, id);
}
});
return o;
}
console.log(getID(data, 112));The running result of the above code is :

边栏推荐
- 网络基础入门理解
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- [Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
- HDR image reconstruction from a single exposure using deep CNNs阅读札记
- Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
- GPS从入门到放弃(十二)、 多普勒定速
- Adjustable DC power supply based on LM317
- ZABBIX proxy server and ZABBIX SNMP monitoring
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
猜你喜欢

UNI-Admin基础框架怎么关闭创建超级管理员入口?
![[leetcode daily clock in] 1020 Number of enclaves](/img/2d/3d12f20c8c73fb28044c01be633c99.jpg)
[leetcode daily clock in] 1020 Number of enclaves

图像的spatial domain 和 frequency domain 图像压缩

Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa

Build op-tee development environment based on qemuv8

重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障

数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据

2500 common Chinese characters + 130 common Chinese and English characters

Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others

0 basic learning C language - interrupt
随机推荐
GPS from getting started to giving up (XV), DCB differential code deviation
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
Unity3d Learning Notes 6 - GPU instantiation (1)
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Management background --4, delete classification
Qt | UDP广播通信、简单使用案例
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Set status bar style demo
GNN,请你的网络层数再深一点~
Chapter 4: talk about class loader again
[leetcode daily clock in] 1020 Number of enclaves
解决项目跨域问题
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
Attack and defense world ditf Misc
GD32F4XX串口接收中断和闲时中断配置
Installation and use of labelimg
lora同步字设置
What are the interface tests? What are the general test points?
中国VOCs催化剂行业研究与投资战略报告(2022版)
Unity3d Learning Notes 6 - GPU instantiation (1)