前端面试题-笔试题

请实现一个fibonacci函数,要求实现以下功能
斐波那契数列:[1,1,2,3,5,8,13,…]
fibonacci(0) => 1
fibonacci(6) => 8

function fibonacci(count) {
    function fn(count, cur = 1, next = 1) {
        if(count === 0) {
            return cur
        } else {
            return fn(count-1, next, cur+next)
        }
    }
    return fn(count)
}
function fibonacci(n) {
    if(n <=1) return 1
    let arr = [1,1]
    let i = n+1-2
    while(i > 0) {
        let a = arr[arr.length-2],
        b = arr[arr.length-1]
        arr.push(a + b)
        i--
    }
    return arr[arr.length -1]
}

输入一个正数N,输出所有和为N的连续正数序列。
例如:输入15,结果[[1,2,3,4,5],[4,5,6],[7,8]

function createArr(n, len) {
    let arr = new Array(len).fill(null), temp = []
    arr[0] = n
    arr = arr.map((item,index) => {
        if(item === null) {
            item = temp[index -1] + 1
        }
        temp.push(item)
        return item
    })
    return arr
}
function fn(count) {
    let result = []
    let middle = Math.ceil(count/2)
    for(let i=1; i count) {
                break
            } else if(total === count) {
                result.push(createArr(i,j))
                break
            }
        }
    }
    return result
}

你可能感兴趣的:(javascript,前端)