-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.RomantoInteger.js
More file actions
45 lines (41 loc) · 835 Bytes
/
13.RomantoInteger.js
File metadata and controls
45 lines (41 loc) · 835 Bytes
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
const romanSymbols = new Map([
[900, 'CM'],
[400, 'CD'],
[90, 'XC'],
[40, 'XL'],
[9, 'IX'],
[4, 'IV'],
[1000, 'M'],
[500, 'D'],
[100, 'C'],
[50, 'L'],
[10, 'X'],
[5, 'V'],
[3, 'III'],
[2, 'II'],
[1, 'I']
]);
// MCMXCIV
// in the string above symbols will be searched
// then we have to remove the symbol from the string
// repeat
function romanToInt(s) {
let myString = s;
let romanToInt = 0;
while(myString != "")
{
romanSymbols.forEach(function logMapElements(value, key, map) {
let position = myString.search(value)
if(position !== -1)
{
console.log(myString)
console.log(value)
myString = myString.replace(value, '')
console.log(myString)
romanToInt += key;
}
});
}
console.log(romanToInt)
};
romanToInt("III")