Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
INCOMPLETE: Convert number into Roman numeral in JavaScript
(function (win,doc) {
'use strict';
var submit = document.getElementById('submit');
submit.addEventListener( 'click', function(){
var number = document.getElementById('number'),
roman_numeral = document.getElementById('roman');
roman_numeral.innerHTML = convert_to_roman(number.value);
}, false);
function convert_to_roman(num) {
let roman = '',
i = 0,
thousands = 0,
hundreds = 0,
remainder = 0;
if ( num < 1 || num > 3999 ) return false;
if ( num > 1000 ) {
thousands = num / 1000;
for( i=1; i < Math.round(thousands); i++) {
roman += 'M';
}
remainder = num % Math.round(thousands) * 1000;
}
if ( remainder > 0 ) {
if ( remainder > 500 ) {
hundreds = remainder / 500;
for( i=1; i < Math.floor(hundreds); i++) {
roman += 'D';
}
remainder = remainder % Math.round(hundreds) * 500;
}
}
return roman;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment