Skip to content

Commit

Permalink
feat: 算法题
Browse files Browse the repository at this point in the history
  • Loading branch information
jinphic committed Jan 4, 2024
1 parent c51c6ce commit ae3c298
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 0 deletions.
22 changes: 22 additions & 0 deletions interview/code/currying.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function add() {
return arguments.reduce((prev, cur) => prev + cur, 0);
}


function currying(fn) {
let args = [];
return function temp() {
const context = this;
if (!arguments.length) {
let ans = fn.apply(context, args);
args = [];
return ans;
} else {
args = [
...args,
...arguments,
];
return temp;
}
}
}
37 changes: 37 additions & 0 deletions interview/code/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function debounce(fn, wait) {
let timer;
return function () {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(this, arguments);
}, wait);
}
}

function debounce_immediate(fn, wait, immediate) {
let timer;
return function () {
let context = this;
let args = arguments;

if (timer) {
clearTimeout(timer);
}

if (!immediate) {
timer = setTimeout(() => {
fn.apply(context, args);
}, wait);
} else {
const callNow = !timer;
timer = setTimeout(() => {
clearTimeout(timer);
}, wait);
if (callNow) {
fn.apply(context, args);
}
}
}
}
47 changes: 47 additions & 0 deletions interview/code/eventbus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class EventBus {
static listener = new Map();

static on = (name, cb) => {
if (this.listener.has(name)) {
this.listener.get(name).push({ once: false, fn: cb });
} else {
this.listener.set(name, [{ once: false, fn: cb }]);
}
}

static once = (name, cb) => {
if (this.listener.has(name)) {
this.listener.get(name).push({ once: true, fn: cb });
} else {
this.listener.set(name, [{ once: true, fn: cb }]);
}
}

static off = (name, cb) => {
if (this.listener.has(name)) {
const cbs = this.listener.get(name).filter(item => item.fn !== cb);
this.listener.set(name, cbs);
}
}

static removeAllListener = (name) => {
if (name) {
this.listener.delete(name);
} else {
this.listener.clear();
}
}

static emit = (name, ...rest) => {
if (this.listener.has(name)) {
const cache = this.listener.get(name);
cache.forEach(item => {
item.fn.call(this, rest);
});
const newCache = cache.filter(item => !item.once);
this.listener.set(name, newCache);
}
}


}
19 changes: 19 additions & 0 deletions interview/code/mySetInterVal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function mySetInterVal(fn, a, b) {
this.a = a;
this.b = b;
this.count = 0;
this.timer = null;
this.start = () => {
this.timer = setTimeout(() => {
fn();
this.count++;
this.start();
}, this.a + this.b * this.count);
}
this.stop = () => {
clearTimeout(this.timer);
this.count = 0;
}
}


74 changes: 74 additions & 0 deletions interview/code/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class MyPromise {
status = 'pending';
data = undefined;
callbacks = [];
constructor(excutor) {

const resolve = (value) => {
if (this.status !== 'pending') {
return;
}
this.status = 'resolved';
this.data = value;
if (this.callbacks.length) {
this.callbacks.forEach(cb => {
cb.onResolved(value);
})
}
}

const reject = (reason) => {
if (this.status !== 'pending') {
return;
}
this.status = 'rejected';
this.data = reason;
if (this.callbacks.length) {
this.callbacks.forEach(cb => {
cb.onRejected(reason);
})
}
}


try {
excutor(resolve, reject)
} catch (error) {// 如果执行器抛出异常将失败
console.error('🙅 error', error);
reject(error)
}
}


then(onResolved, onRejected) {
return new MyPromise((resolve, reject) => {
if (this.status === 'pending') {
this.callbacks.push({ onResolved, onRejected });
} else if (this.status === 'resolved') {
try {
const res = onResolved(this.data);
if (res instanceof MyPromise) {
res.then(
value => resolve(value),
reason => reject(reason),
);
} else {
resolve(res);
}
} catch (reason) {
reject(resolve);
}
} else {
onRejected(this.data)
}
})
}
}



var p = new MyPromise(function (resolve, reject) { resolve(1) });
p.then(
res => console.log(res),
reason => console.error(reason)
)
16 changes: 16 additions & 0 deletions interview/code/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function throttle(fn, wait) {
let timer;
return function () {
const context = this;
const args = arguments;
if (timer) {
return;
}
timer = setTimeout(() => {
fn.apply(context, args);
clearTimeout(timer);
}, wait);
}
}


0 comments on commit ae3c298

Please sign in to comment.