Skip to content

面试过程中遇到的小题目

我印象里的第一道面试题 让我手写的题目

1.实现 在 AABBCCCDDDDDDD 记录每个字母出现的次数?

javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  return words.split("").reduce((a, b) => {
    if (a.hasOwnProperty(b)) {
      return { [b]: a[b]++, ...a };
    } else {
      return { [b]: 1, ...a };
    }
  }, {});
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  for (let i of words) {
    if (obj.hasOwnProperty(i)) {
      obj[i]++;
    } else {
      obj[i] = 1;
    }
  }
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  for (let i = 0; i < words.length; i++) {
    if (obj.hasOwnProperty(words[i])) {
      obj[words[i]]++;
    } else {
      obj[words[i]] = 1;
    }
  }
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  words.split("").forEach((item) => {
    if (obj.hasOwnProperty(item)) {
      obj[item]++;
    } else {
      obj[item] = 1;
    }
  });
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  for (let i in words) {
    if (obj.hasOwnProperty(words[i])) {
      obj[words[i]]++;
    } else {
      obj[words[i]] = 1;
    }
  }
  return obj;
};
javascript
const words = "AABBBCCCDDDEEEAQQ____";
const getNumberLetter = (words) => {
  const obj = {};
  words.split("").map((item) => {
    if (obj.hasOwnProperty(item)) {
      obj[item]++;
    } else {
      obj[item] = 1;
    }
  });
  return obj;
};

2.数组扁平化

javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr) => arr.flat(Infinity);
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr, aa) =>
  arr.reduce((a, b) => {
    if (Array.isArray(b)) {
      return flat(b, a);
    } else {
      return [...a, b];
    }
  }, aa);
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr) => {
  const result = [];
  arr.forEach((item) => {
    if (Array.isArray(item)) {
      result.push(...flat(item));
    } else {
      result.push(item);
    }
  });
  return result;
};
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr, result) => {
  if (!result) {
    result = [];
  }
  return arr.map((item) => {
    if (Array.isArray(item)) {
      return flat(item, result);
    } else {
      return result.push(item);
    }
  });
};
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr, result) => {
  arr.forEach((item) => {
    if (Array.isArray(item)) {
      return flat(item, result);
    } else {
      result.push(item);
    }
  });
  return result;
};
javascript
const arr = [1, [2, [3, [4, 5], 6], 7], 8];
const flat = (arr) => {
  return JSON.parse(
    "[" +
      JSON.stringify(arr).replaceAll("[", "").replaceAll("]", "").split(",") +
      "]"
  );
};

3.数组去重

javascript
const arr = [1, 3, 4, 3, 4, 5];
let arrayDeDuplication = [...new Set(arr)];
javascript
const arrayDeDuplication = (arr) => {
  let result = [];
  arr.forEach((item) => {
    if (!result.includes(item)) {
      result.push(item);
    }
  });
  return result;
};
javascript
const arrayDeDuplication = (arr) => {
  let result = [];
  arr.forEach((item) => {
    if (result.indexOf(item) === -1) {
      result.push(item);
    }
  });
};
javascript
const arrayDeDuplication = (arr) => {
  let result = [];
  arr.forEach((item) => {
    let state = result.filter((it) => it === item);
    if (state.length === 0) {
      result.push(item);
    }
  });
  return result;
};
markdown
主要思路大差不差

- 首先遍历数组
- 判断当前元素是否存在于数组中
- 存在则进入下依次循环 不存在则添加到数组中

4.伪数组转化为数组

javascript
const pseudoArray = [];
Array.form(pseudoArray);
javascript
javascript
javascript

5.debounce(防抖)

6.throttle(节流)

7.函数珂里化

函数接受多个参数为函数的变量

8.继承

javascript
javascript

9.深浅拷贝

10.promise 源码实现思路 实现 all race any 并行方式

11.JSONP

12.图片懒加载

13.滚动加载

14.渲染大量数据 不卡主页面

15.将 VirtualDom 转化为真实 DOM 结构

16.new 操作符

17.字符串 abcde 如何反转

1 . 首先字符串转成数组

  • Array.from()
  • [...new Set()]
  • split
javascript
let str = "helloworld";
let tt = [...str].reduce((a, b) => b + a, "");
javascript
let str = "helloworld";
[...str]
  .reduce((a, b) => {
    a.unshift(b);
    return a;
  }, [])
  .join("");

18.实现将树结构替换字段

javascript
function replaceTreeField(obj, newFiled, oldFiled) {
  for (const key in obj) {
    if (key === oldFiled) {
      obj[newFiled] = obj[key];
      delete obj[key];
    }
    if (obj.hasOwnProperty("children")) {
      obj.children.forEach((child) => {
        replaceWwField(child, newFiled, oldFiled);
      });
    }
  }
}
javascript
function replaceTreeField(obj, newFiled, oldFiled){

}

https://blog.csdn.net/Ed7zgeE9X/article/details/119336745


### 19.将下面的代码补全

```js
const a = (cb) => {
  setTimeout(() => {
    cb(Math.random());
  }, 1000);
};
const b = () => {
  return new Promise((resolve) => {
    a(resolve);
  });
};
b().then((res) => {
  // res是函数a执行后,函数a的参数cb,会接收到的参数
  console.log(res);
});