Skip to content

Commit c9b3d25

Browse files
jonathangomzitsvinayak
authored andcommitted
Create RomanToDecimal.js
Conversion from Roman numeral system to decimal
1 parent 5f961ba commit c9b3d25

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Conversions/RomanToDecimal.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var values = {
2+
'I':1,
3+
'V':5,
4+
'X':10,
5+
'L':50,
6+
'C':100,
7+
'D':500,
8+
'M':1000
9+
};
10+
11+
function romanToDecimal(romanNumber){
12+
let prev = ' ';
13+
14+
let sum = 0;
15+
16+
let newPrev = 0;
17+
for(let i = romanNumber.length - 1; i >= 0; i--){
18+
let c = romanNumber.charAt(i);
19+
20+
if(prev !== ' '){
21+
newPrev = values[prev] > newPrev ? values[prev] : newPrev;
22+
}
23+
24+
let currentNum = values[c];
25+
if(currentNum >= newPrev){
26+
sum += currentNum;
27+
} else {
28+
sum -= currentNum;
29+
}
30+
31+
prev = c;
32+
}
33+
return sum;
34+
}
35+
36+
console.log(romanToDecimal('XXIIVV'));
37+
console.log(romanToDecimal('MDCCCIV'));
38+
console.log(romanToDecimal('XXIVI'));

0 commit comments

Comments
 (0)