The titleJuejin. Cn/post / 693789…
The interview questions
1. Randomize
Function shuffle(arr) {arr.sort(() => math.random () -0.5); function shuffle(arr) {arr.sort(() => math.random () -0.5); return arr; } console.log(shuffle(a));Copy the code
2. The reverse
let str = "I am a student!" ; function reserver(str) { console.log(str.split("").reverse().join("")); } reserver(str);Copy the code
3. To find the intersection
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]);Copy the code
4. Write a 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: "Li Ming ", age: 20}; Let obj2 = {name: "li Ming ", age: 20, 12: function () {}}; MyStringfy (,2,3,4,8 [1])Copy the code
5. Write a throttle function
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);
}
};
}
Copy the code
6. Reject three times
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");
});
Copy the code
Summary of interview questions
- Questions 1, 2, and 3 are ok
addEventListener
Can be called inwindow.onresize=function(){}
, the call failed. Four, six, they don’t know if the code is right.
The written test
String splicing
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 }))
Copy the code
2. Compare character sizes
function sort(str) {
return str
.split("")
.sort((a, b) => {
return b.charCodeAt(0) - a.charCodeAt(0);
})
.join("");
}
console.log(sort("LeBronJames"));
Copy the code
3. Regular substitution
function camel2snake(str) {
return str.replace(/[A-Z]/g, (match) => `_${match.toLowerCase()}`);
}
console.log(camel2snake("fateStayNight"));
Copy the code
4. Array flattening, recursive
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]]]));
Copy the code
5. Use 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"]));
Copy the code
The written summary
- Overall, it’s relatively good. Problem two
charCodeAt
And the third regular is also read the official API to know, still need to learn more practice.