当前位置:网站首页>Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
2022-07-25 23:26:00 【Dragon eyes】
recursive
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.
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));
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(3));
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;
}
边栏推荐
- [wechat applet] page navigation
- Solve the problem phpstudy failed to import the database
- Mongodb features, differences with MySQL, and application scenarios
- Duplicate numbers in array
- Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
- 日期类的实现
- Vscode shortcut key: collapse and expand code
- 谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
- TS basic data type
- Dynamic memory management
猜你喜欢

谷粒学苑P98踩坑 e.GlobalExceptionHandler : null

【代码案例】博客页面设计(附完整源码)

E-commerce RPA, a magic weapon to promote easy entry

WebMvcConfigurationSupport

Secure code warrior learning record (III)

POI特效 市场调研

OASYS system of code audit

Take away applet with main version of traffic / repair to add main access function of traffic

Unity uses macros

How does Navicat modify the language (Chinese or English)?
随机推荐
[wechat applet] page navigation
POI special effects Market Research
js正则表达式匹配ip地址(ip地址正则表达式验证)
Mongodb features, differences with MySQL, and application scenarios
Source code of YY music wechat applet imitating Netease cloud music
Practical skills of easyexcel
POI特效 市场调研
PHP wechat scan code, follow official account and authorize login source code
Redis过期键的删除策略[通俗易懂]
Several commonly used traversal methods
Recommended system - an embedded learning framework for numerical features in CTR prediction
How to set pseudo static for WordPress fixed links
自定义mvc原理
电商RPA,大促轻松上阵的法宝
E-commerce RPA, a magic weapon to promote easy entry
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
Understanding English (speech understanding)
Wrote a little webapi knowledge points from 0 to 1
ETL工具(数据同步) 二
The difference between MySQL clustered index and non clustered index