Skip to content

Commit

Permalink
feat: Menambahkan Algoritma Konversi (Roman to Integer) (#179)
Browse files Browse the repository at this point in the history
* feat: Menambahkan Algoritma Konversi (Roman to Integer)

* fix: return Value + example

* fix: Indentasi
  • Loading branch information
Mgkusumaputra committed Oct 27, 2021
1 parent 7d9b23c commit 571f373
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions algorithm/conversion/RomanToInteger.js
@@ -0,0 +1,40 @@
const romanNum = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};

function romanToInt(num) {
let accumulator = 0;
for (let i = 0; i < num.length; i++) {
if (num[i] === "I" && num[i + 1] === "V") {
accumulator += 4;
i++;
} else if (num[i] === "I" && num[i + 1] === "X") {
accumulator += 9;
i++;
} else if (num[i] === "X" && num[i + 1] === "L") {
accumulator += 40;
i++;
} else if (num[i] === "X" && num[i + 1] === "C") {
accumulator += 90;
i++;
} else if (num[i] === "C" && num[i + 1] === "D") {
accumulator += 400;
i++;
} else if (num[i] === "C" && num[i + 1] === "M") {
accumulator += 900;
i++;
} else {
accumulator += romanNum[num[i]];
}
}
return console.log(accumulator);
}

// Masukan Angka Romawi Disini.
romanToInt("MCMLXXXIX"); // 1989

0 comments on commit 571f373

Please sign in to comment.