-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathromanToArabicNumbers.js
More file actions
36 lines (32 loc) · 876 Bytes
/
romanToArabicNumbers.js
File metadata and controls
36 lines (32 loc) · 876 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
35
36
// Convert roman numeral into arabic
function arabic(x){
let arr = [];
for(let i=0; i<x.length; i++){
if(x[i] === 'I'){
arr.push(1); }else if(x[i] === 'V'){
arr.push(5); }else if(x[i] === 'X'){
arr.push(10); }else if(x[i] === 'L'){
arr.push(50); }else if(x[i] === 'C'){
arr.push(100); }else if(x[i] === 'D'){
arr.push(500); }else{
arr.push(1000);
}
}
let arrSort = [];
for(let i=0; i<arr.length-1; i++){
if(arr[i]>=arr[i+1]){
arrSort.push(arr[i])
}else{
arrSort.push(-arr[i])
}
}
arrSort.push(arr[arr.length-1]);
let jumlah = 0;
for(let i = 0; i<arrSort.length; i++){
jumlah = jumlah + arrSort[i];
}
return jumlah;
}
console.log(arabic('MCLVI')); // result: 1156
console.log(arabic('CCXLIV')); // result: 244
console.log(arabic('DCLXXVIII')); // result: 678