当前位置:网站首页>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 :
边栏推荐
- Search element topic (DFS)
- 自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
- 小程序系统更新提示,并强制小程序重启并使用新版本
- GPS from getting started to giving up (12), Doppler constant speed
- GPS from getting started to giving up (XI), differential GPS
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- Four data streams of grpc
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- Solve project cross domain problems
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
猜你喜欢
PVL EDI 项目案例
Unity3d Learning Notes 6 - GPU instantiation (1)
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
剪映+json解析将视频中的声音转换成文本
[线性代数] 1.3 n阶行列式
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
Search element topic (DFS)
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
随机推荐
做接口测试都测什么?有哪些通用测试点?
GPS从入门到放弃(十二)、 多普勒定速
【10点公开课】:视频质量评价基础与实践
中国VOCs催化剂行业研究与投资战略报告(2022版)
The nearest common ancestor of binary (search) tree ●●
What a new company needs to practice and pay attention to
Management background --3, modify classification
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Memorabilia of domestic database in June 2022 - ink Sky Wheel
pytorch_YOLOX剪枝【附代码】
Codeforces Round #274 (Div. 2) –A Expression
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
【sdx62】WCN685X将bdwlan.bin和bdwlan.txt相互转化操作方法
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Set status bar style demo
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
CCNA-思科网络 EIGRP协议
Build op-tee development environment based on qemuv8
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)