当前位置:网站首页>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 :
边栏推荐
- Oracle control file and log file management
- GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
- Maximum product of three numbers in question 628 of Li Kou
- 自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
- labelimg的安装与使用
- 将MySQL的表数据纯净方式导出
- 每日一题:力扣:225:用队列实现栈
- Lora sync word settings
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- What a new company needs to practice and pay attention to
猜你喜欢
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
ZABBIX proxy server and ZABBIX SNMP monitoring
Assembly and interface technology experiment 5-8259 interrupt experiment
软考高级(信息系统项目管理师)高频考点:项目质量管理
3DMAX assign face map
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
GPS from getting started to giving up (12), Doppler constant speed
[MySQL] online DDL details
How does the uni admin basic framework close the creation of super administrator entries?
随机推荐
Force buckle 575 Divide candy
i. Mx6ull build boa server details and some of the problems encountered
Lora sync word settings
0 basic learning C language - interrupt
0 basic learning C language - digital tube
UNI-Admin基础框架怎么关闭创建超级管理员入口?
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
OpenCV VideoCapture. Get() parameter details
Applet system update prompt, and force the applet to restart and use the new version
QT | UDP broadcast communication, simple use case
Data storage (1)
Mongodb (III) - CRUD
基於 QEMUv8 搭建 OP-TEE 開發環境
Anaconda installs third-party packages
[sciter bug] multi line hiding
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
Attack and defense world miscall
Insert sort and Hill sort
Spatial domain and frequency domain image compression of images
【10点公开课】:视频质量评价基础与实践