-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbreakable.js
44 lines (38 loc) · 915 Bytes
/
unbreakable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function join(arr , sep) {
let res = ""
for (let i = 0; i < arr.length; i++) {
if (i == arr.length-1) {
return res + arr[i]
}
res += arr[i] + sep
}
}
function split(str, sep) {
let res = []
let temp = ''
let sepIndex = 0
if (sep.length === 0) {
for (let index = 0; index < str.length; index++) {
res.push(str[index])
}
return res
}
for (let i = 0; i < str.length; i++) {
if (str[i] === sep[sepIndex]) {
sepIndex++
if (sepIndex === sep.length) {
res.push(temp)
temp = ''
sepIndex = 0
}
} else {
if (sepIndex > 0) {
temp += sep.slice(0, sepIndex)
sepIndex = 0
}
temp += str[i]
}
}
res.push(temp)
return res
}