前端面试题 day2

Scroll Down

第 1 题:多种方式实现斐波那契数列

//基础版斐波那契数列

function fabonacci(n) {
    let num1 = 1,
        num2 = 1,
        sum;
    let arr = [1, 1];
    for (let i = 3; i <= n; i++) {
        sum = num1 + num2;
        num1 = num2;
        num2 = sum;
        arr.push(sum);
    }
    return arr;
}

//基于ES6 Generator实现
function* fabonacciG(num) {
    var a = 1,
        b = 1,
        n = 0;
    while (n < num) {
        yield a;
        [a, b] = [b, a + b];
        n++
    }
}

第 2 题:字符串出现的不重复最长长度

```

let str = 'fdsfsdzffdfdddfsdsds'

let arr = []
const s = str.split('')
let total= 0; // 长度
let maxStr = '' // 最长不重复长度的字符串
for(var i = 0;i <s.length;i++){
const ele = s[i]
const idx = arr.indexOf(ele)
if(idx>-1)

arr.push(ele)
if(arr.length>total){
maxStr = arr.join('')
total = arr.length
}
}

console.log(total)
console.log(maxStr)