-
Notifications
You must be signed in to change notification settings - Fork 0
/
숫자문자열과영단어.js
109 lines (93 loc) · 1.66 KB
/
숫자문자열과영단어.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Solution 1
function translate(eng) {
const number = {
zero: '0',
one: '1',
two: '2',
three: '3',
four: '4',
five: '5',
six: '6',
seven: '7',
eight: '8',
nine: '9',
};
return number[eng];
}
function solution1(s) {
let convertedS;
const arr = s.split('');
const arrN = [];
let eng = '';
for (let i = 0; i < arr.length; i++) {
if (isNaN(arr[i])) {
eng = eng.concat(arr[i]);
if (translate(eng)) {
const num = translate(eng);
arrN.push(num);
eng = '';
}
} else {
arrN.push(arr[i]);
}
}
convertedS = Number(arrN.join(''));
return convertedS;
}
// Solution 2
function solution2(s) {
const numbers = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
];
numbers.forEach((number, idx) => {
s = s.split(number).join(idx);
});
return Number(s);
}
// Solution 3
function solution3(s) {
const numbers = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
];
let convertedS = s;
numbers.forEach((number, index) => {
convertedS = convertedS.split(number).join(index);
});
convertedS = parseInt(convertedS);
return convertedS;
}
// Solution 4
function solution4(s) {
const convertedS = parseInt(
s
.replace(/zero/g, 0)
.replace(/one/g, 1)
.replace(/two/g, 2)
.replace(/three/g, 3)
.replace(/four/g, 4)
.replace(/five/g, 5)
.replace(/six/g, 6)
.replace(/seven/g, 7)
.replace(/eight/g, 8)
.replace(/nine/g, 9)
);
return convertedS;
}