-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.js
More file actions
43 lines (38 loc) · 1.07 KB
/
RomanToInteger.js
File metadata and controls
43 lines (38 loc) · 1.07 KB
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
const ROMAN_NUMBERS = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
var romanToInt = function(s) {
var result = 0;
var lastDigitComputed = false;
const arrayOfRomans = s.split("");
const arrayOfNumbers = arrayOfRomans.map(roman => ROMAN_NUMBERS[roman]);
for (let index = 0; index < arrayOfNumbers.length - 1; index++){
if (arrayOfNumbers[index] >= arrayOfNumbers[index+1]){
result += arrayOfNumbers[index];
}
else {
result += (arrayOfNumbers[index+1] - arrayOfNumbers[index])
if(index == arrayOfNumbers.length-2){
lastDigitComputed = true;
}
index++;
}
}
if(lastDigitComputed == false){
result += arrayOfNumbers[arrayOfNumbers.length-1];
}
return result;
};
console.log( romanToInt('XI') );
console.log( romanToInt('XIII') );
console.log( romanToInt('CI') );
console.log( romanToInt('IX') );
console.log( romanToInt('MCIX') );
console.log( romanToInt('MCMXCIV') );
console.log( romanToInt('MDCXCV') );