Last active
March 11, 2018 17:38
-
-
Save stevenpray/711c3a088d01755d8face34e5a8dbd2a to your computer and use it in GitHub Desktop.
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
'use strict'; | |
function toromans(number) { | |
const decimals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; | |
const romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]; | |
let result = ''; | |
for (var i = 0; i <= decimals.length; i++) { | |
const decimal = decimals[i]; | |
const roman = romans[i]; | |
const remain = number % decimal; | |
while (remain < number) { | |
number -= decimal; | |
result += roman; | |
} | |
} | |
return result; | |
} | |
console.log(toromans(9)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment