# 实现下面代码

function repeat(s, count) {}
repeat("s", 3); // 输出 ‘sss’
1
2
  • 实现下面代码
// 正则匹配标签名 输出div p span
<div>
  <p>
    <span></span>
  </p>
  <span></span>
</div>
1
2
3
4
5
6
7
  • 请实现 find 函数,使下列的代码调用正确。
// 约定:
// title数据类型为String
// userId为主键,数据类型为Number
var data = [
  { userId: 8, title: "title1" },
  { userId: 11, title: "other" },
  { userId: 15, title: null },
  { userId: 19, title: "title2" },
];
var find = function(origin) {
  // your code are here...
};
// 查找 data 中,符合条件的数据,并进行排序
var result = find(data)
  .where({
    title: /\d$/,
  })
  .orderBy("userId", "desc");

console.log(result); // [{ userId: 19, title: 'title2'}, { userId: 8, title: 'title1' }];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  • 实现 sendRequest(promises, max, callback),同时最多执行 max 个 promises,超过的等待有空闲的开始执行,执行完成后执行 callback
function sendRequest(promises, max, callback) {
  let current = 0;
  const results = [];
  const originLen = promises.length;
  const next = () => {
    while (current < max && promises.length) {
      const index = originLen - promises.length;
      const promise = promises.shift();
      results[index] = {
        value: null,
        reason: null,
      };
      Promise.resolve(promise)
        .then(
          (value) => {
            results[index].value = value;
          },
          (reason) => {
            results[index].reason = reason;
          }
        )
        .finally(() => {
          current--;
          next();
        });
      current++;
    }

    if (current === 0) {
      callback(results);
    }
  };
  next();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  • 实现 findFirstIndex 函数,找到有序数组 [1, 2, 3, 4, 7, 7, 7, 9, 12, 23, 34, 45, 55, 67]中第一次出现的位置,比如 7 第一次出现的位置是 4
function findFirstIndex(arr, target) {
  let begin = 0;
  let end = arr.length;
  while (begin < end) {
    const mid = (begin + end) >>> 1;
    if (arr[mid] >= target) {
      end = mid;
    } else {
      begin = mid + 1;
    }
  }
  if (begin === arr.length) return -1;
  return arr[begin] === target ? begin : -1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  • 十进制转换成任意进制
function tenToOther(num, base) {
  const baseNumber = "0123456789abcdefghijklmnopqrstuvwxyz";
  const result = [];
  while (num) {
    const rest = num % base;
    num = Math.floor(num / base);
    result.unshift(baseNumber[rest]);
  }
  return result.join("");
}
1
2
3
4
5
6
7
8
9
10
  • 大数相加
function bigAdd(a, b) {
  const aArr = a.split("");
  const bArr = b.split("");

  let flag = 0;
  let result = [];
  while (aArr.length || bArr.length) {
    const left = aArr.pop() || 0;
    const right = bArr.pop() || 0;

    const value = Number(left) + Number(right) + flag;

    result.unshift(value % 10);

    flag = parseInt(value / 10);
  }

  if (flag) {
    result.unshift(flag);
  }

  return result.join("");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 深拷贝
function deepClone(obj, map = new WeakMap()) {
  if (obj === null || typeof obj !== "object") {
    return obj;
  }
  if (map.has(obj)) {
    return map.get(obj);
  }
  const copy = Array.isArray(obj) ? [] : {};
  map.set(obj, copy);
  const keys = Reflect.ownKeys(obj);
  keys.forEach((key) => {
    copy[key] = deepClone(obj[key], map);
  });
  return copy;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 图片懒加载写代码
function lazyload() {
  const observe = new IntersectionObserver((enteris) => {
    enteris.forEach((entry) => {
      const lazyImage = entry.target;
      if (
        entry.isIntersecting &&
        lazyImage.getAttribute("src") == "loading.gif"
      ) {
        lazyImage.src = lazyImage.dataset.src;
        observe.unobserve(lazyImage);
      }
    });
  });

  for (let i = 0; i < imgs.length; i++) {
    observe.observer(imgs[i]);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 相关文章

手撕前端面试代码题