题目juejin.cn/post/693789…
面试题
1.随机打乱
function shuffle(arr) {
arr.sort(() => Math.random() - 0.5);
return arr;
}
console.log(shuffle(a));
2.反转
let str = "I am a student!";
function reserver(str) {
console.log(str.split("").reverse().join(""));
}
reserver(str);
3.找交集
function fun(arg) {
let args = arguments;
let result = [];
let flag = false;
[...args].map((item, index) => {
minArr = item;
if (item.length < minArr) {
minArr = item;
}
});
minArr.map((item, index) => {
flag = [...args].every((ite) => ite.includes(item));
flag && result.push(item);
});
console.log(result);
return result;
}
fun([1, 2, 3], [2, 3, 4], [2, 3], [1, 3, 5]);
4.手写JSON.stringfy
const stringOrOtherVal = (data) => {
if (typeof data === "string") {
return `"${data}"`;
} else {
return `${data}`;
}
};
const myStringfy = (params)=> {
let resultStr = "{";
for (let key in params) {
resultStr += `"${key}":${stringOrOtherVal(params[key])},`;
}
resultStr = resultStr.slice(0, resultStr.length - 1);
resultStr += "}";
console.log(resultStr);
return resultStr;
};
let obj1 = { nane: "李明", age: 20 };
let obj2 = { name: "李明", age: 20, 12: function () {} };
myStringfy([1,2,3,4,8])
5.写一个节流函数
let btn = document.getElementById("btn");
function logs() {
console.log(1);
}
btn.addEventListener("click", debounce(logs, 500), false);
function resize(func, wait) {
let timeout;
return function () {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(this, arguments);
}, wait);
}
};
}
6.三次失败走reject
let myAxios = function (option) {
console.log("myAxios");
return new Promise((resolve, reject) => {
setTimeout(() => {
reject("error");
}, 1000);
});
};
function runAxios(option, retry = 2) {
let p = myAxios(option);
if (retry > 0) {
retry--;
return new Promise((resolve, reject) => {
p.then(resolve).catch(() => {
resolve(runAxios(option, retry));
});
});
}
return p;
}
runAxios({
url: "123/12",
})
.then(() => {
console.log("success");
})
.catch((err) => {
console.log("err");
});
面试题总结
- 第1,2,3题还可以,第5题只有在
addEventListener
中才可以调用,在window.onresize=function(){}
,调用失败。第4,6完全不会,也不知道代码对不对。
笔试
字符串拼接
function toString(map) {
return Object.keys(map)
.map((item, index) => {
return (item = `${item}=${map[item]}`);
})
.join("&");
}
console.log(toString({ a: 1, b: 2, c: 3 }))
2.比较字符大小
function sort(str) {
return str
.split("")
.sort((a, b) => {
return b.charCodeAt(0) - a.charCodeAt(0);
})
.join("");
}
console.log(sort("LeBronJames"));
3.正则替换
function camel2snake(str) {
return str.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
}
console.log(camel2snake("fateStayNight"));
4.数组扁平化,递归
function flattenDeep(list) {
var flattenArr = [];
for (var item of list) {
if (typeof item == "number") {
flattenArr.push(item);
} else {
flattenArr = flattenArr.concat(flattenDeep(item));
}
}
return flattenArr;
}
function flattenDeep1(list) {
return list.flat(Infinity);
}
console.log(flattenDeep([1, 2, [2, [3, [4], 5]]]));
console.log(flattenDeep1([1, [2, [3, [4], 5]]]));
5.hash使用
function count(list) {
const map = new Map();
for (var str of list) {
if (map.has(str)) {
map.set(str, map.get(str)+1);
} else {
map.set(str, 1);
}
}
return Object.fromEntries(map);
}
console.log(count(["a", "a", "b", "c", "b", "a"]));
笔试总结
- 总体相对来说还行,第二题的
charCodeAt
和第三题 正则也是看了官方api才知道,还得多学多练。
发表评论
还没有评论,快来抢沙发吧!