当前位置:网站首页>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 :
边栏推荐
- Maximum product of three numbers in question 628 of Li Kou
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- Management background --4, delete classification
- CCNA-思科网络 EIGRP协议
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- NetXpert XG2帮您解决“布线安装与维护”难题
- 软考高级(信息系统项目管理师)高频考点:项目质量管理
- 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
- 2500 common Chinese characters + 130 common Chinese and English characters
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
猜你喜欢
Xiaoman network model & http1-http2 & browser cache
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
Oracle control file and log file management
【sciter】: 基于 sciter 封装通知栏组件
Crawler obtains real estate data
Installation and use of labelimg
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
LeetCode刷题(十一)——顺序刷题51至55
2022年6月国产数据库大事记-墨天轮
3DMax指定面贴图
随机推荐
OpenCV VideoCapture. Get() parameter details
Build op-tee development environment based on qemuv8
将MySQL的表数据纯净方式导出
pytorch_YOLOX剪枝【附代码】
What is the difference between animators and animators- What is the difference between an Animator and an Animation?
Search element topic (DFS)
GPS from getting started to giving up (12), Doppler constant speed
Qt | UDP广播通信、简单使用案例
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
GNN,请你的网络层数再深一点~
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
Chapter 3: detailed explanation of class loading process (class life cycle)
Classic sql50 questions
微信红包封面小程序源码-后台独立版-带测评积分功能源码
Support multiple API versions in flask
Unity3d Learning Notes 6 - GPU instantiation (1)
解决项目跨域问题
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
Management background --1 Create classification