-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem71.js
More file actions
25 lines (19 loc) · 689 Bytes
/
problem71.js
File metadata and controls
25 lines (19 loc) · 689 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
/*
Implement a function that converts a Roman numeral to an integer. The function should take a Roman numeral string (e.g., "IX" or "XXI") as input and return the corresponding integer value.
*/
function romanToIntNum(romanNum) {
let numInt = 0;
const romValues = {M:1000, D:500, C:100, L:50, X:10, V:5, I:1};
for(let i = 0; i < romanNum.length; i++){
if(romValues[romanNum[i]] < romValues[romanNum[i+1]]){
numInt -= romValues[romanNum[i]];
}
else{
numInt += romValues[romanNum[i]]
}
}
return numInt;
}
console.log(romanToIntNum("CXM"));
console.log(romanToIntNum("XX"));
console.log(romanToIntNum("VI"));