For the front end, I use javascript、 Node Js and so on, and occasionally write articles.
I like to share what I learn.
个人技术博客
For the front end, I use javascript、 Node Js and so on, and occasionally write articles.
I like to share what I learn.
let obj = {
"aa.bb": 1,
"bb.cc.dd": 2
}
function set() {
// ...
}
console.log(set(obj));
// 要求输出
// {
// aa: {
// bb: 1
// },
// bb: {
// cc: {
// dd: 2
// }
// }
// }
function objectIs (a, b) {
...
}
objectIs(+0, -0); // false
objectIs(NaN, NaN); // true;
objectIs(null, null); // true;
objectIs(undefined, undefined); // true;
// 实现一个GreetRobot,使之具有以下能力
// 1. 执行GreetRobot('bb8')
// 输出: Hi this is bb8!
// 2. 执行GreetRobot('bb8').greet('Bob')
// 输出: Hi this is bb8!
// Nice to meet you Bob.
// 3. 执行GreetRobot('bb8').greet('Bob').greet('Jerry')
// 输出: Hi this is bb8!
// Nice to meet you Bob.
// Nice to meet you Jerry.
// 4. 执行GreetRobot('bb8').greet('Bob').greetVip('Mike')
// 输出: Glad to see you Mike~
// Hi this is bb8!
// Nice to meet you Bob.
// 5. 执行GreetRobot('bb8').greetVip('Marry').greetVip('Mike')
// 输出: Glad to see you Mike~
// Glad to see you Marry~
// Hi this is bb8!
new Queue()
.task(1000,()=>console.log(1))
.task(2000,()=>console.log(2))
.task(3000,()=>console.log(3)).start();
实现一个Queue函数,调用start之后,1s后打印1,接着2s后打印2,然后3s后打印3
function findIndex(arr, target) {
}
let arr = [2,0,1,0,0,3,2,10,11,8,7,3,5,4,4,5,3,9];
findIndex(arr, 2); // 6
findIndex(arr, 4); // 11
// 接收一个number,返回一个string
function format(number) {
}
console.log(format(12345.7890)); // 12,345.789,0
console.log(format(0.12345678));// 0.123,456,78
console.log(format(123456)); // 123,456
class EventEmitter {
constructor(){
}
// 监听事件
on(){
}
// 触发事件
emit(){
}
// 只监听一次,下次emit不会触发
once(){
}
// 移除事件
off(){
}
}
// 输入一个数组,请你将【异位词】组合在一起。可以按任意顺序返回结果列表。
// 异位词:是由相同的字母重新组成的词,所有源单词中的字母通常恰好只用一次。
// 输入:strs = ['abc', 'bac', 'cbd']
// 输出:[['abc', 'bac'], ['cbd']]
function getWords() {
// ...
}
getWords(['abc', 'bac', 'cbd'])
const fs = require('fs');
// 使用前
fs.readFile('./index.js', 'utf-8', (err, data) => {
if(!err) {
console.log(data.toString())
}
console.log(err);
});
// 使用promisify后
const readFile = promisify(fs.readFile)
readFile('./index.js')
.then(data => {
console.log(data.toString())
})
.catch(err => {
console.log('error:', err)
});
function promisify(func) {
// ...
}
let a = "123456789012345678";
let b = "1";
function add(a ,b){
//...
}
add(a, b) // '123456789012345679'
实现一个flat函数,扁平化数组,接收一个arr数组和一个depth参数,如果不传,默认为1;
function flat (arr, depth) {
}
flat([1,[2,3,[4,5]]], 2); // [1,2,3,4,5]
flat([1,[2,3,[4,5]]]); // [1,2,3,[4,5]]
示例 1:
输入:s = "3[a]2[bc]"
输出:"aaabcbc"
示例 2:
输入:s = "3[a2[c]]"
输出:"accaccacc"
示例 3:
输入:s = "sens2[abc]3[cd]ef"
输出:"sensabcabccdcdcdef"
示例 4:
输入:s = "11[c2[b]]"
输出:"cbbcbbcbbcbbcbbcbbcbbcbbcbbcbbcbb"
function uncompress(s) {
...
}
请完善 koa.js
const Koa = require('./koa.js');
const app = new Koa();
app.use(async (ctx, next) => {
console.log('1');
next();
console.log('2');
});
app.use(async (ctx, next) => {
console.log('3');
next();
console.log('4');
});
app.use(async (ctx, next) => {
console.log('5');
next();
console.log('6');
});
app.listen(3000);
// 打印 1 3 5 6 4 2
// koa.js
function koa() {
...
}
请手动实现一个函数的防抖和节流,并且说明使用场景?
Class Scheduler{
constructor(){}
}
function timeout(time){
return new Promise(resolve=>{
setTimeout(resolve,time)
})
}
var scheduler = new Scheduler()
function addTask(time,order){
scheduler
.add(()=>timeout(time))
.then(()=>console.log(order))
}
addTask(1000,1)
addTask(500,2)
addTask(300,3)
addTask(400,4)
//要求
// ouput : 2 3 1 4
//一开始1,2俩个任务进入队列
//500ms时,2完成,输出2,任务3进入队列
//800ms时,3完成,输出3,任务4进入队列
//1000ms时,1完成,输出1
//1200ms时,4完成,输出4
/**
* @description: 根据传入的姓名权重信息,返回随机的姓名(随机概率依据权重)
* @param {Array} personValue
* @returns {String} personName 姓名
*/
var getPersonName = function (personValue) {
}
const person = [
{
name: '张三',
weight: 1
},
{
name: '李四',
weight: 10
},
{
name: '王五',
weight: 100
}
]
function getResult(count){
const res = {}
for(let i = 0; i < count; i++){
const name = getPersonName(person)
res[name] = res[name] ? res[name] + 1 : 1
}
console.log(res)
}
getResult(10000)
let middlewares = [
(str) => {
return str + '1'
},
(str) => {
return str + '2'
},
(str) => {
return str + '3'
},
(str) => {
return str + '4'
}
]
function compose(arr) {
...
}
const add = compose(middlewares);
add('hello'); // hello4321
// 给定一个数组list,编写函数getList,要求先生成一个树形结构,然后再跟据树形结构生成列表result
let list = [
{id:1,pid:0},
{id:2,pid:0},
{id:3,pid:1},
{id:4,pid:1},
{id:5,pid:4},
{id:6,pid:3},
{id:7,pid:5},
{id:8,pid:7}
];
let result = [
{id:1,pid:0,path:'-0-'},
{id:2,pid:0,path:'-0-'},
{id:3,pid:1,path:'-0-1-'},
{id:4,pid:1,path:'-0-1-'},
{id:5,pid:4,path:'-0-1-4-'},
{id:6,pid:3,path:'-0-1-3-'},
{id:7,pid:5,path:'-0-1-4-5-'},
{id:8,pid:7,path:'-0-1-4-5-7-'}
];
function getList(){
//
}
getList(list)
Array.prototype.map = function(callback, thisArg) {
}
const sleep = (delay, order) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(order)
}, delay)
})
}
const p = () => {
return 'p';
}
let arr = [
sleep(1000, 0),
p(),
sleep(1300, 3),
sleep(1200, 2),
]
Promise.all = function (promises) {
...
}
Promise.all(arr)
.then((res) => {
console.log(res); // [ 0, 'p', 3, 2 ]
})
let obj = {
a: 1,
b: [1, 2, {c: {d: true} }, [3], {e: [6, 7, 8] }],
f: {g: 2, h: 3},
i: null
}
function flatten(obj) {
...
}
flatten(obj)
// 输出
// {
// 'a': 1,
// 'b[0]': 1,
// 'b[1]': 2,
// 'b[2].c.d': true,
// 'b[3][0]': 3,
// 'b[4].e[0]': 6,
// 'b[4].e[1]': 7,
// 'b[4].e[2]': 8,
// 'f.g': 2,
// 'f.h': 3,
// 'i': null
// }
提供了一个RedPackage
的类,初始化时传入红包金额和个数,需要实现一个openRedPackage
方法,每调一次都进行一次“抢红包”,并以console.log
的形式输出抢到的红包金额。
function sleep(delay, name) {
let startTime = new Date().getTime();
console.log(delay, name, 'start')
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(delay, name, new Date().getTime() - startTime, 'end --------')
resolve({
delay,
name
});
}, delay * 1000)
})
}
function asyncPool({limit, items}) {
// todo
}
asyncPool({
limit: 2,
items: [
() => sleep(1, '吃饭'),
() => sleep(3, '睡觉'),
() => sleep(5, '打游戏'),
() => sleep(3.5, '学习算法'),
() => sleep(4, '学习Vue跟React')
]
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.