前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两道笔试面试题目

两道笔试面试题目

作者头像
flytam
发布2020-01-14 17:24:53
3140
发布2020-01-14 17:24:53
举报

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

代码语言:javascript
复制
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);
代码语言:javascript
复制
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);
代码语言:javascript
复制
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);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档