笔试题及答案(答案是我自己写的)
function findTopSum(arr, k, m) {
const temp = Array.from(new Set(arr));
temp.sort((a, b) => a - b);
const len = temp.length;
if (m > len || k > len) return;
const max = temp[len - k];
const min = temp[len - m];
let num = 0;
arr.forEach((a) => {
if (a === max || a === min) num += a;
});
return num;
}
function adjoin(arr, condition) {
const temp = [];
let temp2 = [];
arr.forEach((a, i) => {
if (condition(a)) {
temp2.push(a);
if (arr.length === i + 1) {
temp.push(temp2);
temp2 = [];
}
return;
}
if (temp2.length) {
temp.push(temp2);
temp2 = [];
}
temp.push(a);
});
return temp;
}
function EatMan(name) {
return new _eatman(name);
}
function _eatman(name) {
this.arr = [];
this.arr.push({
type: "EatMan",
name: name,
});
this.eat = (name) => {
this.arr.push({
type: "eat",
name: name,
});
this.next();
return this;
};
this.eatFirst = (name) => {
this.arr.unshift({
type: "eatFirst",
name: name,
});
this.next();
return this;
};
this.next = () => {
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.arr.forEach((a) => {
switch (a.type) {
case "EatMan":
console.log(`Hi! This is ${a.name}!`);
break;
default:
console.log(`Eat ${a.name}~`);
break;
}
});
}, 100);
};
return this;
}