Skip to content

数组求和

70 字小于 1 分钟

js算法

2024-12-28


let list = [1,2,3,4,5,6]

方法一:
function f(i) {
  return i >=list.length ? 0:list[i]+f(i+1)
}

方法二:
function fl(i) {
    let list = [1,2,3,4,5,6]
    let res;
    for(let n=0;i<list.length; i++) {
        n+=list[i]
        res = n
    }
    return res
}