Skip to content

Commit

Permalink
feat: 0801 handwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
niaogege committed Aug 2, 2023
1 parent 3331b96 commit 297d0e2
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/interview/experiences/0717writing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* 12.flatter 扁平化手写
* 13.手写发布订阅模式
* 14.instanceof 手写
* 15.手写选择排序和选择排序
* 15.手写选择排序和插入排序
* 16.手写二分法
* 17.手写驼峰转换
* 18.手写防抖和节流
Expand All @@ -37,7 +37,8 @@
* 37.链表是否有环?
* 38.缓存memo函数
* 39.手写函数重载
* 40.前中后序遍历
* 40.二叉树前中后序遍历(迭代方式)
* 41.冒泡排序和归并排序
**/

// > 定个小目标100个手写提
Expand Down
1 change: 0 additions & 1 deletion docs/interview/experiences/practise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
9.16进制转rgb or rgb转16进制
10.mockMap/mockFilter 数组方法重写
11.mockMap
//////////
12.mockReduce
13.pipe
14.ajax
Expand Down
127 changes: 127 additions & 0 deletions docs/interview/practise/0731.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* 1.asyncPool
* 2.promisify 把回调函数转成 promise 形式
* 3.手写jsonp
* 4.手写ajax
* 5.手写async/await
* 6.手写遍历器
* 7.URL参数解析
* 8.手写插入和选择排序
* 9.前中后序遍历
* 10.手写useFetch
*/

async function asyncPool(limit, array, fn) {
var res = [];
var executing = [];
for (let i = 0; i < array.length; i++) {
var p1 = Promise.resolve().then(() => fn(array[i], array));
res.push(p1);
if (array.length >= limit) {
var p2 = p1.then(() => {
return executing.splice(executing.indexOf(p2), 1);
});
executing.push(p2);
if (executing.length >= limit) {
await Promise.race(executing);
}
}
}
return Promise.all(res);
}

class Iterator {
constructor(obj) {
this.obj = obj;
this.index = 0;
this.length = Object.keys(obj).length;
}
[Symbol.iterator]() {
return this;
}
next() {
return this.length > this.index
? { value: this.obj[this.index++], done: false }
: { done: true, value: undefined };
}
}

Promise.prototype.allSettled = function (arr) {
var res = [];
return new Promise((resolve, reject) => {
for (let i = 0; i < arr.length; i++) {
Promise.resolve(arr[i]).then(
(val) => {
var value = {
value: val,
status: 'fulfilled',
};
res[i] = value;
if (arr.length === i + 1) {
resolve(res);
}
},
(err) => {
var value = {
reason: err,
status: 'rejected',
};
res[i] = value;
if (arr.length === i + 1) {
reject(res);
}
},
);
}
});
};

Promise.prototype.myAllSettled2 = function (arr) {
return Promise.all(
arr.map((e) => {
return Promise.resolve(e).then(
(res) => {
return { status: 'fulfilled', value: res };
},
(err) => {
return { status: 'rejected', reason: err };
},
);
}),
);
};

// 插入排序
function insertSort(arr) {}

// 选择排序
function selectSort(arr) {}

//回调函数转成 promise 形式
function promisify(fn) {
return (...rest) => {
return new Promise((resolve, reject) => {
fn(rest[0], function (err, con) {
if (err) {
reject(err);
} else {
resolve(con);
}
});
});
};
}

function promisify2(fn) {
return (...rest) =>
new Promise((resolve, reject) => {
rest.push(function (err, con) {
if (err) {
reject(err);
} else {
resolve(con);
}
});
fn.apply(this, rest);
});
}
158 changes: 158 additions & 0 deletions docs/interview/practise/0801.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* 1.千分位展示
* 2.驼峰大小写
* 3.手写插入和选择排序
* 4.flatten扁平化
* 5.bubbleSort 冒泡排序
* 6.promisify
* 7.二叉树的前中后需迭代遍历
* 8.url参数拼接
* 9.ajax手写
* 10.instanceof
*/

// ajax
function ajax(url, option, cb) {
var xml = new XMLHttpRequest();
xml.open('GET', url, true);
xml.onreadystatechange = function () {
if (xml.readyState === xml.DONE) {
if (xml.status === 200) {
cb(xml.responseText);
}
}
};
xml.send();
}

// 用于检测构造函数的原型是否在某一个实例的原型链上
Function.prototype.instanceof = function (l, r) {
while (l) {
if (l === r.prototype) {
return true;
}
l = l.__proto__;
}
return false;
};

Function.prototype.mockInstanceOf = function (l, r) {
return r.prototype.isPrototypeof(l);
};

class Promise {
constructor(exe) {
this.cbs = [];
this.data = undefined;
const resolve = (val) => {
setTimeout(() => {
this.data = val;
this.cbs.forEach((cb) => cb(val));
});
};
exe(resolve);
}
then(onFulfilled) {
return new Promise((resolve, reject) => {
this.cbs.push(() => {
var res = onFulfilled(this.data);
if (res instanceof Promise) {
res.then(resolve);
} else {
resolve(res);
}
});
});
}
}

function thousand(str) {
var reg = /(?!^)(?=(\d{3})+$)/g;
return str.replace(reg, ',');
}
thousand('123456789');

function upperCase(str) {
return str.replace(/[-|@|_]([\w])/g, (match, p) => p.toUpperCase());
}
upperCase('cpp-wmh');

// 插入 从后往前 一一对比
function insertSort(arr) {
var pre;
var len = arr.length;
for (let i = 1; i < len; i++) {
pre = i - 1;
var cur = arr[i];
while (pre >= 0 && arr[pre] > cur) {
arr[pre + 1] = arr[pre];
pre--;
}
arr[pre + 1] = cur;
}
return arr;
}
insertSort([33, 22, 11, 2, 334, 2, 99]);

// 选择排序 选择最小的交换
function selectSort(arr) {
let minIndex, temp;
len = arr.length;
for (let i = 0; i < len - 1; i++) {
minIndex = i;
for (let j = i + 1; j < len; j++) {
if (arr[j] <= arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
selectSort([33, 22, 11, 2, 334, 2, 99]);

function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] >= arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
bubbleSort([22, 33, 11, 2, 99, 88, 7]);

function flatten(arr) {
var stack = [...arr];
var res = [];
while (stack.length) {
var last = stack.pop();
if (Array.isArray(last)) {
stack.push(...last);
} else {
res.push(last);
}
}
return res.reverse();
}
flatten([1, [2, [4, [55, 66, 7], 9]]]);

function promisify(fn) {
return new Promise((resolve, reject) => {
return (...rest) => {
rest.push(function (err, con) {
if (err) {
reject(err);
} else {
resolve(con);
}
});
fn.apply(this, rest);
};
});
}

0 comments on commit 297d0e2

Please sign in to comment.