Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dbfaf0e

Browse files
committed
update
1 parent 7e6111f commit dbfaf0e

10 files changed

+135
-123
lines changed

‎Topics/二维数组组合.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// 2021秋招-富途一面
22

3-
// 2.二维数组组合:输入:[['red', 'green'], ['a1', 'a2'], ['b1', 'b2']]
3+
// 二维数组组合:输入:[['red', 'green'], ['a1', 'a2'], ['b1', 'b2']]
44
// 编写一个函数,要求输出:['red-a1-b1', 'red-a1-b2', 'red-a2-b1', 'red-a2-b2',
55
// 'green-a1-b1', 'green-a1-b2','green-a2-b1','green-a2-b2']
66

77
const arr = [['red', 'green'], ['a1', 'a2'], ['b1', 'b2']];
88

99
// 方法一:
10+
const create = arr => arr.reduce((a, b) => a.map(aItem => b.map(bItem => aItem + '-' + bItem)).reduce((a, b) => [...a, ...b]))
11+
1012
function create(arr) {return arr.reduce((a, b) => a.map(aItem => b.map(bItem => aItem + '-' + bItem)).reduce((a, b) => [...a, ...b]))}
1113

1214
function create(arr) {

‎Topics/关键字搜索树&数组元素分类.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 2021秋招-深信服一面
22

3-
// 1.关键字搜索树:写一个过滤函数,输入一个树和一个字符串,如:
3+
// 关键字搜索树:写一个过滤函数,输入一个树和一个字符串,如:
44
tree = [
55
{'name': 'A'},
66
{'name': 'B', 'children': [
@@ -45,7 +45,7 @@ function filter(tree, str) {
4545
return result
4646
}
4747

48-
// 2.数组元素分类:写一个函数,将
48+
// 数组元素分类:写一个函数,将
4949
arr = ['2018年1月1日', '2018年1月5日', '2019年7月17日', '2020年4月19日', '2020年4月30日', '2020年11月25日']
5050
// 转化为:
5151
obj = {

‎Topics/判断数据类型&直线任务.js

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// 判断数据类型
2+
function type(obj) {
3+
if (Number.isNaN(obj)) return 'NaN';
4+
if (Array.isArray(obj)) return 'Array';
5+
if (obj === null) return 'Null';
6+
let res = typeof obj;
7+
return res === 'object' ? Object.prototype.toString.call(obj).replace('[object ', '').replace(']', '') : res.replace(res[0], res[0].toUpperCase());
8+
}
9+
10+
console.log(type(123)); // Number
11+
console.log(type('123')); // String
12+
console.log(type(false)); // Boolean
13+
console.log(type(['123'])); // Array
14+
console.log(type(null)); // Null
15+
16+
// 2021秋招-百度一面
17+
// 实现计数器函数:使用函数时数字加 1,使用其 clean 方法可以归零。
18+
19+
// 方法一(不需要全局变量):
20+
const add1 = (function() {
21+
let num = 0;
22+
function log() {
23+
num++;
24+
console.log(num);
25+
}
26+
log.clean = () => num = 0
27+
return log
28+
})()
29+
30+
add1(); // 1
31+
add1(); // 2
32+
add1(); // 3
33+
add1.clean();
34+
add1(); // 1
35+
36+
// 方法二:
37+
const add2 = (function() {
38+
let num = 0;
39+
// const that = this;
40+
function log() {
41+
num++;
42+
console.log(num);
43+
}
44+
log.clean = function () {
45+
num = 0
46+
}
47+
return log
48+
})()
49+
50+
add2(); // 1
51+
add2(); // 2
52+
add2(); // 3
53+
add2.clean();
54+
add2(); // 1
55+
56+
// 方法三(需要用到全局变量):
57+
let num = 0;
58+
function add3() {
59+
num++;
60+
console.log(num);
61+
}
62+
add3.clean = () => num = 0;
63+
64+
add3(); // 1
65+
add3(); // 2
66+
add3(); // 3
67+
add3.clean();
68+
add3(); // 1
69+
70+
// 方法四(需要用到全局变量):
71+
var num = 0;
72+
function add4() {
73+
num++;
74+
console.log(num);
75+
}
76+
add.clean = () => this.num = 0; // 这里不用 this 也可以,但用了 this 前面定义 num 必须用 var
77+
78+
add4(); // 1
79+
add4(); // 2
80+
add4(); // 3
81+
add4.clean();
82+
add4(); // 1

‎Topics/合并对象&时间计算.js renamed to ‎Topics/合并对象&计算时间.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 2021秋招-哔哩哔哩笔试
22

3-
// 1.合并对象:合并 2 个对象以及其子对象,返回一个新的对象,如:
3+
// 合并对象:合并 2 个对象以及其子对象,返回一个新的对象,如:
44
// { a: 1 } { a: 2 },返回 { a: [1,2] }
55
// { a: 1 } { b: 1 },返回 { a: 1, b: 1 }
66
// { a: [1] } { a: [2] },返回 { a: [1,2] }
@@ -21,7 +21,7 @@ console.log(arr)
2121
console.log(JSON.parse(JSON.stringify(obj1)), JSON.parse(JSON.stringify(obj2)))
2222
console.log(key1, val1, key2, val2)
2323

24-
// 2.时间计算:写一个函数接收语义化的描述,得到计算后的日期时间。
24+
// 计算时间:写一个函数接收语义化的描述,得到计算后的日期时间。
2525
// PS: (暂不考虑【月】【年】的计算)
2626
// +/-3W 加或减3周,+/-3d 加或减3天,+/-3h 加或减3小时
2727
// +/-3m 加或减3分钟,+/-3s 加或减3秒

‎Topics/奶酪迷宫.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 2021秋招-金山云笔试
22

3-
// 2.一只聪明的老鼠发现了一个三角形的奶酪迷宫。这个迷宫有若干个小房间,每个小房间有若干
3+
// 一只聪明的老鼠发现了一个三角形的奶酪迷宫。这个迷宫有若干个小房间,每个小房间有若干
44
// 块大小相等的小奶酪(每个房间至少有一块奶酪)。奶酪迷宫的构造如下:(1)奶酪迷官一共
55
// 有 N 行,第 1 行有一个小房间,第 2 行有两个房间,第 3 行有三个小房间,...第N行有N个小房间。
66
// (2)所有的小房间都是从第1列开始进行排列的。(3)奶酪迷宫的入口是第 1 行的那个小房间。

‎Topics/打乱数组&setInterval.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
// 2021秋招-阅文笔试
22

33
// 使用原生代码打乱一个 JS 数组
4+
// 参考:https://juejin.cn/post/6844903863812620296 。
45

5-
// 用 setTimeout 实现 setInterval
6+
// 用 setTimeout 实现 setInterval
7+
// 参考:https://juejin.cn/post/6844903839934447629 。
8+
9+
// 简单版
10+
const mySetInterval = (cb, time) => {
11+
function fn() {
12+
cb();
13+
setTimeout(arguments.callee, time);
14+
}
15+
setTimeout(() => fn(), time);
16+
return fn
17+
}
18+
19+
// 升级版
20+
let timeMap = {};
21+
let id = 0; // 简单实现 id 唯一
22+
const mySetInterval = (cb, time) => {
23+
let timeId = id; // 将 timeId 赋予 id
24+
id++; // id 自增实现唯一 id
25+
function fn() {
26+
cb()
27+
timeMap[timeId] = setTimeout(() => fn(), time)
28+
}
29+
timeMap[timeId] = setTimeout(fn, time);
30+
return timeId; // 返回timeId
31+
}
32+
33+
mySetInterval(() => console.log(new Date()), 1000);
34+
35+
// 3. 用 clearTimeout 实现 clearInterval
36+
37+
const myClearInterval = (id) => {
38+
clearTimeout(timeMap[id]); // 通过 timeMap[id] 获取真正的id
39+
delete timeMap[id];
40+
}

‎Topics/数组最大公因数.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎Topics/url获取key的value值&字符串相邻去重.js renamed to ‎Topics/获取url的key对应值&字符串相邻去重.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// 2021秋招-小红书笔试
22

3-
// 1.url 获取 key 的 value 值
3+
// url 获取 key 的 value 值
44
function solveMeFirst1(url, key) {
55
let search = '&';
66
if (!url.includes(search)) search = '?';
@@ -18,14 +18,16 @@ console.log(solveMeFirst1('https://xiaohongshu.com?ans=984', 'ans')); // 984
1818
console.log(solveMeFirst1('https://xiaohongshu.com?name=619&key=88&ans=984', 'kkk')); // false
1919
console.log(solveMeFirst1('https://xiaohongshu.com?name=619&key=88&ans=984', 'name')); // 619
2020

21-
// 2.字符串相邻去重
21+
// 字符串相邻去重
2222
function solveMeFirst2(str) {
2323
let res = '';
2424
for (let i = 0; i < str.length; i++) {
25-
if (str[i] !== str[i + 1]) res += str[i]
25+
if (str[i] !== str[i + 1]) {
26+
res += str[i];
27+
}
2628
}
2729
return res;
2830
}
2931

30-
console.log(solveMeFirst3('AAAABBBCCCDDDAABBB')); // ABCDAB
31-
console.log(solveMeFirst3('fewwffffwerreroao')); // fewfwereroao
32+
console.log(solveMeFirst2('AAAABBBCCCDDDAABBB')); // ABCDAB
33+
console.log(solveMeFirst2('fewwffffwerreroao')); // fewfwereroao

‎Topics/数组去重排序&转驼峰.js renamed to ‎Topics/转驼峰&最大公因数.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
// 2021秋招-创蓝云智笔试
22

3-
// 数组去重排序
4-
function sortWith(arr) {
5-
arr = Array.from(new Set(arr));
6-
arr = arr.sort((num1, num2) => {
7-
if (num1 > num2) return 1;
8-
else if (num1 < num2) return -1;
9-
else return 0;
10-
})
11-
return arr;
12-
}
13-
14-
sortWith([5, 4, 9, 4, 1, 55, 2]); // [1, 2, 4, 5, 9, 55]
15-
163
// 转驼峰
174
function change(str) {
185
for (let i = 0; i < str.length; i++) {
@@ -54,16 +41,6 @@ function change2(str) {
5441

5542
change2('a-bcd-ef-gh-i'); // 'aBcdEfGhI'
5643

57-
// 九九乘法口诀表
58-
function print99() {
59-
for (let i = 1; i <= 9; i++) {
60-
const resArr = [];
61-
for (let j = 1; j <= i; j++) {
62-
const res = i + ×ばつ' + j + '=' + i*j;
63-
resArr.push(res);
64-
}
65-
console.log(...resArr);
66-
}
67-
}
44+
// 2021秋招-ZOOM笔试
6845

69-
print99();
46+
// 给你一个数组,求出数组最大公因数

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /