-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_from_roman_to_decimal.js
61 lines (55 loc) · 1.26 KB
/
09_from_roman_to_decimal.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
/**
=====================================
Converting Roman Numerals to Decimal
INFO -> https://it.wikipedia.org/wiki/Sistema_di_numerazione_romano
=====================================
Given a Roman numeral, the task is to find its
corresponding decimal value.
Examples
---
Input: IX
Output: 9
---
Input: XL
Output: 40
---
Input: MCMIV
Output: 1904
---
Ⅰ = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1 000
**************************************************/
const romanMap = new Map();
romanMap.set("I", 1);
romanMap.set("V", 5);
romanMap.set("X", 10);
romanMap.set("L", 50);
romanMap.set("C", 100);
romanMap.set("D", 500);
romanMap.set("M", 1000);
const convertRomanToDecimal = (romanNumber) => {
// WRITE YOUR CODE HERE
const romanNumberArray = romanNumber.split("");
const result = romanNumberArray.map((el) => romanMap.get(el));
let total = 0;
for (let i = 0; i < result.length; i++) {
let prevIndex;
if (i > 0) {
prevIndex = i - 1;
}
if (prevIndex !== "undefined" && result[i] > result[prevIndex]) {
total += result[i] - 2 * result[prevIndex];
} else {
total += result[i];
}
}
return total;
};
console.log(convertRomanToDecimal("IX"));
console.log(convertRomanToDecimal("XL"));
console.log(convertRomanToDecimal("MCMIV"));