当前位置:网站首页>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 :
边栏推荐
- SQL Server生成自增序号
- 2500个常用中文字符 + 130常用中英文字符
- 3DMax指定面贴图
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- GNN, please deepen your network layer~
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- Powerful domestic API management tool
- 每日一题:力扣:225:用队列实现栈
- 2022年6月国产数据库大事记-墨天轮
- Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
猜你喜欢
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
Oracle-控制文件及日志文件的管理
[线性代数] 1.3 n阶行列式
Search element topic (DFS)
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
GNN,请你的网络层数再深一点~
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
【10点公开课】:视频质量评价基础与实践
pytorch_YOLOX剪枝【附代码】
【sciter】: 基于 sciter 封装通知栏组件
随机推荐
GPS from entry to abandonment (XIV), ionospheric delay
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
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
QT | UDP broadcast communication, simple use case
2500 common Chinese characters + 130 common Chinese and English characters
414. The third largest digital buckle
十二、启动流程
Powerful domestic API management tool
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
What are the interface tests? What are the general test points?
【10点公开课】:视频质量评价基础与实践
Applet system update prompt, and force the applet to restart and use the new version
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
Force deduction question 500, keyboard line, JS implementation
基於 QEMUv8 搭建 OP-TEE 開發環境
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
Solve project cross domain problems
Mongodb (III) - CRUD
Memorabilia of domestic database in June 2022 - ink Sky Wheel
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks