-
-
Save anonymous/9da3df57bfbaa21a9d00 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/piotrjaw 's solution for Bonfire: Roman Numeral Converter
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
| // Bonfire: Roman Numeral Converter | |
| // Author: @piotrjaw | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-roman-numeral-converter?solution=function%20convert(num)%20%7B%0A%20%20var%20map%20%3D%20%5B%20%0A%20%20%20%20%5B1000%2C%20%27M%27%5D%2C%0A%20%20%20%20%5B900%2C%20%27CM%27%5D%2C%0A%20%20%20%20%5B500%2C%20%27D%27%5D%2C%0A%20%20%20%20%5B400%2C%20%27CD%27%5D%2C%0A%20%20%20%20%5B100%2C%20%27C%27%5D%2C%0A%20%20%20%20%5B90%2C%20%27XC%27%5D%2C%0A%20%20%20%20%5B50%2C%20%27L%27%5D%2C%0A%20%20%20%20%5B40%2C%20%27XL%27%5D%2C%0A%20%20%20%20%5B10%2C%20%27X%27%5D%2C%0A%20%20%20%20%5B9%2C%20%27IX%27%5D%2C%0A%20%20%20%20%5B5%2C%20%27V%27%5D%2C%0A%20%20%20%20%5B4%2C%20%27IV%27%5D%2C%0A%20%20%20%20%5B1%2C%20%27I%27%5D%0A%20%20%5D%3B%0A%20%20%0A%20if%20(num%20%3E%200)%20%7B%0A%20%20%20var%20i%20%3D%200%3B%0A%20%20%20while%20(num%20%2F%20map%5Bi%5D%5B0%5D%20%3C%201)%20%7B%0A%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20return%20map%5Bi%5D%5B1%5D%20%2B%20convert(num%20-%20map%5Bi%5D%5B0%5D)%3B%0A%20%7D%20else%20%7B%0A%20%20%20return%20%27%27%3B%0A%20%7D%0A%7D%0A%0Aconvert(1)%3B%0A | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function convert(num) { | |
| var map = [ | |
| [1000, 'M'], | |
| [900, 'CM'], | |
| [500, 'D'], | |
| [400, 'CD'], | |
| [100, 'C'], | |
| [90, 'XC'], | |
| [50, 'L'], | |
| [40, 'XL'], | |
| [10, 'X'], | |
| [9, 'IX'], | |
| [5, 'V'], | |
| [4, 'IV'], | |
| [1, 'I'] | |
| ]; | |
| if (num > 0) { | |
| var i = 0; | |
| while (num / map[i][0] < 1) { | |
| i++; | |
| } | |
| return map[i][1] + convert(num - map[i][0]); | |
| } else { | |
| return ''; | |
| } | |
| } | |
| convert(1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment