QUIZ

1. 實現css佈局
一個div垂直居中
其距離屏幕左右兩邊各10px
其高度始終是寬度的50%css

div中有文本'A'
其font—size:20px
文本水平垂直居中算法

<style>
    .wrapper {
      margin: 0 10px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: pink;
    }
  </style>

<body>
  <div class="wrapper">
    <div class="font">A</div>
  </div>
</body>

ps: 經試驗 其高度始終是寬度的50% 這個沒有實現數組

2.函數中的arguments是數組嗎?類數組轉數組的方法瞭解一下?promise

arguments類數組
var array = [...arguments]閉包

3.類型比較app

if([]==false){console.log(1)};
if({}==false){console.log(2)};
if([]){console.log(3)}
if([1]==[1]){console.log(4)}異步

都不打印async

4.EventLoop函數

async function a1 () {
    console.log('a1 start')
    await a2()
    console.log('a1 end')
}
async function a2 () {
    console.log('a2')
}

console.log('script start')

setTimeout(() => {
    console.log('setTimeout')
}, 0)

Promise.resolve().then(() => {
    console.log('promise1')
})

a1()

let promise2 = new Promise((resolve) => {
    resolve('promise2.then')
    console.log('promise2')
})

promise2.then((res) => {
    console.log(res)
    Promise.resolve().then(() => {
        console.log('promise3')
    })
})
console.log('script end')

打印:
script start
a1 start
a2
a1 end
script end
promise1
promise2
promise2.then
promise3
setTimeoutoop

promise2錯了,知道爲何,可是不知道爲啥a1 end是在異步代碼執行後打印的

5.改正代碼,輸出0123401234

function a () {
    for (var i = 0; i < 5; i++) {
        this.i = i
        setTimeout(function () {
            console.log(i)
        }, 0)
        console.log(this.i)
    }
}

a()

將var改爲let 考察閉包
可是好像是錯的....爲何改爲let會中間出現undefined.....
改爲let後:
01234undefined01234

6.手寫bind實現

Function.prototype.bind2 = function (context) {

    var self = this;
    // 得到bind的參數從第二個參數到最後一個參數
    var args = Array.prototype.slice.call(arguments, 1);

    var fNOP = function () {};

    var fBound = function () {
        // 指bind返回的函數傳入的參數
        var bindArgs = Array.prototype.slice.call(arguments);
        // new bind返回的函數,this失效,但傳入的參數生效
        return self.apply(this instanceof fNOP ? this : context, args.concat(bindArgs));
    }
    // 保證繼承,原型鏈,下面兩行代碼等同於Object.creater()  fbound.prototype = Object.create(this.prototype);
    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();
    return fBound;
}

7.看這個圖,個人理解是,持續觸發事件,每隔一段時間,只執行一次事件,事件節流

clipboard.png

function throttle(func, wait) {
    var context, args;
    var previous = 0;

    return function() {
        var now = +new Date();
        context = this;
        args = arguments;
        if (now - previous > wait) {
            func.apply(context, args);
            previous = now;
        }
    }
}

//調用
元素.onmousemove = throttle(func, 100);

8.從一個無序,不相等的數組中,選取N個數,使其和爲M實現算法
算法題涼.....我感受這道題應該和二叉樹有關......

9.一個字典['I', 'have', 'a', 'book', 'good'],實現一個函數,判斷一個字符串中是否都是出自字典中的,輸出true/false
算法題涼.....
笨方法:

var arr = ['I', 'have', 'a', 'book', 'good']
var str = 'I have a book'

function test(str,arr) {
    return arr.filter(v => str.indexOf(v) !== -1).length === str.split(' ').length
}

10.一個長階梯有n級,能夠一次走1級,一次走2級,一共有多少種走法?

function test (n) {
    if (n === 1) return 1
    if (n === 2) return 2
    return test(n - 1) + test(n - 2)
}

用笨方法作的....先寫出來n爲1,2,3,4,5時的走法,看出是用遞歸

相關文章
相關標籤/搜索