Created
December 3, 2015 02:29
-
-
Save anonymous/dc290e6fd2ab826a765f to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Roman Numeral Converter
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
// Bonfire: Roman Numeral Converter | |
// Author: @nirajkrz | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-roman-numeral-converter | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function convert(num) { | |
if (!+num) | |
return false; | |
var digits = String(+num).split(""), | |
key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM", | |
"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC", | |
"","I","II","III","IV","V","VI","VII","VIII","IX"], | |
roman = "", | |
i = 3; | |
while (i--) | |
roman = (key[+digits.pop() + (i * 10)] || "") + roman; | |
return Array(+digits.join("") + 1).join("M") + roman; | |
} | |
convert(36); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment