forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToDecimal.js
More file actions
Latest commit
34 lines (28 loc) · 534 Bytes
/
Copy pathRomanToDecimal.js
File metadata and controls
34 lines (28 loc) · 534 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
constvalues={
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
exportfunctionromanToDecimal(romanNumber){
letprev=' '
letsum=0
letnewPrev=0
for(leti=romanNumber.length-1;i>=0;i--){
constc=romanNumber.charAt(i)
if(prev!==' '){
newPrev=values[prev]>newPrev ? values[prev] : newPrev
}
constcurrentNum=values[c]
if(currentNum>=newPrev){
sum+=currentNum
}else{
sum-=currentNum
}
prev=c
}
returnsum
}