Created
May 9, 2013 12:28
Final conversion details
This file contains 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
function checkDef( val ) { | |
if( val === undefined ) { | |
throw new Error( 'Number out of range' ); | |
} | |
} | |
function _doConvert( arabic, single, five, ten ) { | |
var i = parseInt( arabic, 10 ); | |
switch( i ) { | |
case 1: return single; | |
case 2: return single + single; | |
case 3: return single + single + single; | |
case 4: checkDef( five ); return single + five; | |
case 5: checkDef( five ); return five; | |
case 6: checkDef( five ); return five + single; | |
case 7: checkDef( five ); return five + single + single; | |
case 8: checkDef( five ); return five + single + single + single; | |
case 9: checkDef( ten ); return single + ten; | |
case 0: return ""; | |
} | |
if( isNaN(i) || i+"" != arabic+"" ) | |
{ | |
throw new Error( 'Cannot convert' ); | |
} | |
throw new Error( 'Number out of range' ); | |
} | |
var App = { | |
... | |
convertArabicToRoman = function(arabic){ | |
try{ | |
var thousands = _doConvert( Math.floor(arabic / 1000), "M" ); | |
arabic = arabic % 1000; | |
var hundreds = _doConvert( Math.floor(arabic / 100), "C", "D", "M" ); | |
arabic = arabic % 100; | |
var tens = _doConvert( Math.floor(arabic / 10), "X", "L", "C" ); | |
arabic = arabic % 10; | |
return thousands + hundreds + tens + _doConvert( arabic, "I", "V", "X" ); | |
} catch( e ) { | |
return e.message; | |
} | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment