字符串中连续的第一组三个数字
2023-06-16 10:29:54
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