Last active
December 18, 2019 12:11
-
-
Save bartcis/5dede6c8dbf34b8c16b0f9874c7e69a2 to your computer and use it in GitHub Desktop.
Convert Arabic numbers to Roman notation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Conver Arabic to Roman Numbers | |
function romanConventerBasic(number) { | |
// 1. Create array with roman numbers and arabic equivalents | |
const decimalValue = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; | |
const romanValue = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']; | |
// 2. New variable that will be romanized number | |
let romanized = ''; | |
// 3. Map through numbers array and comapre against your number | |
decimalValue.map((a, i) => { | |
// 4. As long as your number is bigger than value in an array | |
while (a <= number) { | |
// 5. Add corresponding roman character to the string | |
romanized += romanValue[i]; | |
// 6. And reduce your number by a value from arabic array. | |
number -= a; | |
} | |
}); | |
return romanized; | |
} | |
console.log(romanConventerBasic(2345)); // MMCCCXLV |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment