-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromantointeger.js
79 lines (62 loc) · 1.37 KB
/
romantointeger.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
s = "MCMXCIV"
let roman = new Map([
["I", 1],
["II", 2],
["III", 3],
["IV", 4],
["V", 5],
["IX", 9],
["X", 10],
["XL", 40],
["L", 50],
["XC", 90],
["C", 100],
["CD", 400],
["D", 500],
["CM", 900],
["M", 1000]
])
const arr = s.split('');
let numb = 0
let convert = []
for (let i = 0; i < arr.length;) {
value1 = arr[i]
value2 = arr[i + 1]
special = value1 + value2
if ((special) === ("IV")) {
i += 2
console.log(special)
convert.push(special)
} else if ((special) === ("IX")) {
i += 2
console.log(special)
convert.push(special)
} else if ((special) === ("XL")) {
i += 2
console.log(special)
convert.push(special)
} else if ((special) === ("XC")) {
i += 2
console.log(special)
convert.push(special)
} else if ((special) === ("CD")) {
i += 2
console.log(special)
convert.push(special)
} else if ((special) === ("CM")) {
i += 2
console.log(special)
convert.push(special)
} else {
i++
console.log(value1)
convert.push(value1)
}
}
for (let j = 0; j < convert.length;) {
numb += roman.get(convert[j])
console.log(numb)
j++
}
console.log(convert)
// problem url https://leetcode.com/problems/roman-to-integer/