两道笔试面试题目

  • 2020 年 1 月 14 日
  • 筆記

1、实现类似如下效果 Human(“Tam”) // log Tam .sleep(1000)// wait 1000 log sleep 1000 .say(“hhh”)// log hhh .sleep(2000); // sleep 2000 log 2000 主要就是考察对于js事件循环的应用。

function Human(name) {    console.log(name);    const queue = [];    setTimeout(async () => {      let index = 0;      while (queue[index]) {        await queue[index++]();      }    }, 0);    return {      say: function(content) {        queue.push(() => {          console.log(content);          return Promise.resolve();        });        return this;      },      sleep: function(times) {        queue.push(() => {          return new Promise(resolve => {            setTimeout(() => {              console.log("sleep", times);              resolve();            }, times);          });        });        return this;      }    };  }  Human("Tam")    .sleep(1000)    .say("hhh")    .sleep(2000);
function render(str) {    return function(obj) {      return str.replace(/${(w+)}/g, function(match, p1) {        return obj[p1] ? obj[p1] : "";      });    };  }  const year = "2017";  const month = "09";  const day = "21";  const str = render("${year}-${month}-${day}")({ year, month, day });  console.log(str);
function copy(m) {    const mLength = ("" + m).length;    let result = [];    let b = Math.floor(mLength / 2);    for (let i = 10; i < 10 ** b; i++) {      const number =        String(i) +        String(i)          .split("")          .reverse()          .join("");      result.push(number);      for (let j = 0; j <= 9; j++) {        result.push(          String(i) +            String(j) +            String(i)              .split("")              .reverse()              .join("")        );      }    }    result = result.filter(function(item) {      return item <= m;    });    console.log(result);  }    copy(20000);