当前位置:网站首页>js array recursively use
js array recursively use
2022-08-02 13:12:00 【Beijing carries the handle】
Fibonacci sequence:
1,1,2,3,5,8,13,21,34...
function fi(n){if(n == 1 || n == 2) return 1;return fi(n-1) + fi(n-2)}console.log(fi(3)) // 2
Array sum:
function sum(arr){if(arr.length == 0){return 0}else if(arr.length == 1){return arr[0]}else{return arr[0] + sum(arr.slice(1))// slice(1) intercepts the data from the first to the end of the array}}console.log(sum([1,2,3])) // 6
Array flattening:
const res = [];const fn = arr => {for (let i = 0; i < arr.length; i++) {if (Array.isArray(arr[i])) {fn(arr[i]);} else {res.push(arr[i]);}}}fn([1,[2,3]]);
边栏推荐
猜你喜欢
随机推荐
This binding to detailed answers
自动生成代码器推荐-code-gen
How to use the database like tap water?|Tencent Cloud Database TDSQL-C
Closures in JS
this的绑定指向详细解答
FreeRTOS创建任务--动态创建、静态创建
设置代理服务器(谷歌+IE)「建议收藏」
Redis all
js cool dashboard plugin
js九宫格样式抽奖插件
【C语言】剖析函数递归(3)
ETL(二):表达式组件的使用
读《IDEO,设计改变一切》有感
js semi-circle loading progress animation js special effects
C语言结构体(入门)
最小割和对偶图(未完成)
Intouch System Platform IDE-1
一款强大的js弹出alert插件
86.(cesium之家)cesium叠加面接收阴影效果(gltf模型)
Scala基础语法入门(三)Scala中的各种运算符