数字马力 笔试
2023-04-14 15:27:07
字符串转小驼峰
ts
const str = 'background-image'
function toLower(s) {
const result = s.split('-')
let res = ''
console.log(result)
result.map((item, index) => {
if (index > 0) {
item.split('').map((it, idx) => {
if (idx > 0) {
res += it
} else {
res += it.toLocaleUpperCase()
}
})
} else {
res += item
}
})
return res
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
数组拼接
- 略
实现flat 递归
ts
const _flatten = arr => {
const res = []
arr.map(item => {
if (Array.isArray(item)) {
res.push(..._flatten(item))
} else {
res.push(item)
}
})
return res
}
// console.log(_flatten([1,[2,[3,[4]]]]))
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
找到字符串中连续的第一组三个数字
ts
function captureThreeNumbers(str) {
let res = ''
for (let i = 0; i < str.length; i++) {
if (
Number(str[i] + 1) === Number(str[i + 1]) &&
Number(str[i] + 2) === Number(str[i + 2])
) {
console.log('升序')
res = str[i] + str[i + 1] + str[i + 2]
return res
} else if (
Number(str[i] - 1) === Number(str[i + 1]) &&
Number(str[i] - 2) === Number(str[i + 2])
) {
console.log('降序')
res = str[i] + str[i + 1] + str[i + 2]
return res
}
}
}
// console.log(captureThreeNumbers('9876543'))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
深拷贝
ts
function deepClone(source) {
const target = {}
for (property in source) {
if (typeof source[property] === 'object') {
target[property] = deepClone(source[property])
} else {
target[property] = source[property]
}
}
return target
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11