美文网首页
撸代码的题

撸代码的题

作者: 行走的蛋白质 | 来源:发表于2020-06-08 09:59 被阅读0次

      1. 给定: const arr1 = [1, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9];
        输出: ['1-3', 3, '3-7,', 7, '7-9']
    function trans(arr) {
      let start = 0;
      let end = 0;
      const result = [];
      for (let i = 0; i < arr.length; i++) {
        end = i;
        if (arr[i + 1] - arr[i] !== 1) {
          if (start === end) {
            result.push(arr[i]);
          } else {
            result.push(`${arr[start]}-${arr[end]}`);
          }
          start = i + 1;
        }
      }
      return result;
    }
    console.log(trans(arr1));
    

    • 5.实现一个 sleep 函数,比如 sleep(1000) 意味着等待1000毫秒
    function sleep1(wait) {
        return new Promise((resolve, reject) => {
            setTimeout(resolve, wait)
        })
    }
    sleep1(1000).then(_ => {
        console.log('promise sleep')
    })
    
    function sleep2(wait) {
        return new Promise((resolve, reject) => {
            setTimeout(resolve, wait)
        })
    }
    async function asyncawait(wait) {
        await sleep2(wait)
        console.log('async await sleep')
    }
    asyncawait(1000)
    
    function sleep3(fun, wait) {
        if(fun.constructor === Function) {
            setTimeout(fun, wait)
        }
    }
    function es5sleep() {
        console.log('es5 sleep')
    }
    sleep3(es5sleep, 1000)
    
    function* sleep4(wait) {
        yield new Promise((resolve, reject) => {
            setTimeout(resolve, wait)
        })
    }
    let s4 = sleep4(1000)
    s4.next().value.then(_ => {
        console.log('generator sleep')
    })
    

    var a = 10;
    (function () {
        console.log(a)
        a = 5
        console.log(window.a)
        var a = 20;
        console.log(a)
    })()
    

    • 3.下面代码中 a 在什么情况下会打印 1?
    let a = {
        v: 1,
        /*toString() {
            return a.v++
        }*/
        valueOf() {
            return a.v++
        }
    }
    if( a == 1 && a == 2 && a == 3 ) {
      console.log(1);
    }
    

    • 2.使用迭代的方式实现 flatten 函数,也就是数组扁平化
    var arr=[1,2,3,[4,5],[6,[7,[8]]]]
    
    function flat1() {
        let result = []
        return function flat(arr) {
            arr.forEach(o => {
                if(o.constructor === Array) {
                    result.concat(flat(o))
                } else {
                    result.push(o)
                }
            })
            return result
        }
    }
    console.log(flat1()(arr))
    
    function flat2(arr) {
        return arr.reduce((pre, cur) => {
            return cur.constructor === Array ? pre.concat(flat2(cur)) : pre.concat(cur)
        }, [])
    }
    console.log(flat2(arr))
    

      1. 请把俩个数组 [A1, A2, B1, B2, C1, C2, D1, D2] 和 [A, B, C, D],合并为 [A1, A2, A, B1, B2, B, C1, C2, C, D1, D2, D]
    let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
    let arr2 = ['A', 'B', 'C', 'D']
    
    function comArr(arr1, arr2) {
        let num = 0
        while(arr2.length) {
            let firstEle = arr2.shift()
            for(let i = num; i <= arr1.length; i++) {
                if(arr1[i] === undefined) {
                    arr1.push(firstEle)
                    break
                }
                if(!arr1[i].includes(firstEle)) {
                    num = i + 1
                    arr1.splice(i, 0, firstEle)
    //                debugger
                    break;
                }
            }
            
        }
        return arr1
    }
    
    console.log(comArr(arr1, arr2))
    

    相关文章

      网友评论

          本文标题:撸代码的题

          本文链接:https://www.haomeiwen.com/subject/jstazhtx.html