-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex38.js
56 lines (46 loc) · 1.19 KB
/
ex38.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
45
46
47
48
49
50
51
52
53
54
55
56
function isUpperCase(code) {
return (code >= 65 && code <= 90)
}
function isLetter(code) {
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
}
function words(str = '', patternopt, flagsopt) {
const array = []
let column = 0
if (patternopt) {
const regex = new RegExp(patternopt, flagsopt)
return str.match(regex) || []
}
for (let i = 0; i < str.length; i++) {
const code = str.charCodeAt(i)
const previousCode = str.charCodeAt(i - 1)
if (
str[i] === ' ' &&
isLetter(previousCode)
) {
column++
continue
}
if (
isUpperCase(code) &&
str[i - 1] !== ' ' &&
str[i - 1] !== undefined
) {
column++
}
if (array[column]) {
array[column] += str[i]
} else {
if (str[i] !== '-') {
array[column] = str[i]
}
}
}
return array
}
console.log(words('Tony Tony Ch0pper'))
// => ['Tony', 'Tony', 'Ch0pper']
console.log(words('TonyTonyCh0pper'))
// => ['Tony', 'Tony', 'Ch0pper']
console.log(words('Tony - Tony - Ch0pper!'))
// => ['Tony', 'Tony', 'Ch0pper']