Skip to content

Instantly share code, notes, and snippets.

@stevenpray
Last active March 11, 2018 17:38
Show Gist options
  • Save stevenpray/711c3a088d01755d8face34e5a8dbd2a to your computer and use it in GitHub Desktop.
Save stevenpray/711c3a088d01755d8face34e5a8dbd2a to your computer and use it in GitHub Desktop.
'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