- 排序,使用冒泡排序实现
function sort(paramsArray) {
for (let index = 0; index < paramsArray.length - 1; index++) {
for (let j = index + 1; j < paramsArray.length; j++) {
if (paramsArray[j] < paramsArray[index]) {
[paramsArray[j], paramsArray[index]] = [
paramsArray[index],
paramsArray[j]
];
}
}
}
return paramsArray;
}
const sort1 = sort([5, 4, 3, 2, 1]);
console.assert(sort1[0] === 1);
console.assert(sort1[1] === 2);
console.assert(sort1[2] === 3);
console.assert(sort1[3] === 4);
console.assert(sort1[4] === 5);
const sort2 = sort([]);
console.assert(sort2.length === 0);
const sort3 = sort([1, 1, 1]);
console.assert(sort3[0] === 1);
console.assert(sort3[2] === 1);
- 斐波那契数列,每一步的结果缓存起来不会重复计算,效率大大提升
// 2 缓存
function name(previous, next, nowN, n) {
if (nowN === n) {
return previous + next;
} else {
return name(next, previous + next, ++nowN, n);
}
}
function fibonacci(n) {
console.time("计算斐波那契数列第50项");
let previous = 0;
let next = 1;
if (n === 0) {
return previous;
}
if (n === 1) {
return next;
}
const finalNum = name(previous, next, 2, n);
console.timeEnd("计算斐波那契数列第50项");
return finalNum;
}
console.assert(fibonacci(0) === 0);
console.assert(fibonacci(1) === 1);
console.assert(fibonacci(2) === 1);
console.assert(fibonacci(3) === 2);
console.assert(fibonacci(4) === 3);
console.assert(fibonacci(5) === 5);
console.assert(fibonacci(6) === 8);
console.assert(fibonacci(20) === 6765);
console.assert(fibonacci(50) === 12586269025);
- Promise,感觉偏简单了,是不是我做得不对?
// 3 Promise
function light(timer, fn) {
return new Promise((res, rej) => {
if (timer) {
setTimeout(() => {
fn();
res();
}, timer * 1000);
} else {
rej();
}
});
}
function red() {
console.log(new Date());
console.log("red");
}
function green() {
console.log(new Date());
console.log("green");
}
function yellow() {
console.log(new Date());
console.log("yellow");
}
light(1, red)
.then(function() {
return light(2, green);
})
.then(function() {
return light(3, yellow);
});
- ==的会做,通过修改toString实现,===的一开始没思路,后来搜了下做出来了
// 4 Hack
let num = 1;
let now = typeof window === "undefined" ? global : window;
Object.defineProperty(now, "a", {
get() {
return num++;
},
set(newValue) {
now.a = newValue;
}
});
console.assert(a === 1 && a === 2 && a === 3);
- 深拷贝,也很快做出来了
// 5 深拷贝
function clone(o) {
let newObject = {};
for (const key in o) {
if (o.hasOwnProperty(key)) {
const element = o[key];
if (Object.prototype.toString.call(element) === "[object Object]") {
newObject[key] = clone(element);
} else {
newObject[key] = o[key];
}
}
}
return newObject;
}
const o1 = {
date: new Date(2020, 0, 1),
fn: function() {
return "fn";
},
array: [1, 2, 3],
obj: {
nested: {
name: "frank",
date2: new Date(2020, 0, 1),
fn2: function() {
return "fn2";
}
}
}
};
const o2 = clone(o1);
console.assert(o2.date.valueOf() === o1.date.valueOf());
console.assert(o2.fn() === "fn");
console.assert(o2.array[2] === 3);
console.assert(o2.obj.nested.name === "frank");
console.assert(o2.obj.nested.date2.valueOf() === o1.obj.nested.date2.valueOf());
console.assert(o2.obj.nested.fn2() === "fn2");
o1.obj.nested.name = "jack";
console.assert(o2.obj.nested.name === "frank");
- 由于前两天恰好看了下井字棋教程,所以大概了解react的用法了,但是input那里不知道怎么操作,所以特意去看了下,也没花太多时间就写完主要功能了,唯一耗时多点的地方就是字典排序那里,对数据的结构做了些调整
// 6 CRUD
// 字典排序
// 实现详见 https://codesandbox.io/s/funny-curie-4pczn?file=/src/App.js
function setWord() {
let newArray = [
["jbc", "d"],
["zbc", "d"],
["cbc", "d"]
];
let now = {};
let indexArray = [];
newArray.forEach(ele => {
now[ele[0]] = ele;
indexArray.push(ele[0]);
});
indexArray = indexArray.sort();
let newNameArray = [];
indexArray.forEach(element => {
newNameArray.push(now[element]);
});
return newNameArray;
}
setWord();