Roman numeral Converter
1. Requirements
- Converts a given number into Roman numerals
- All returned Roman numerals should be in uppercase form
2. Ideas
- Array of corresponding Roman numerals for single-digit, 10-bit, hundred, thousand, respectively
- Use Math.floor () to remove numbers from each bit of the number, and to find the corresponding Roman numerals in the previously defined array.
3. Code
function convert(num) {var arr1 = [‘‘,‘I‘,‘II‘,‘III‘,‘IV‘,‘V‘,‘VI‘,‘VII‘,‘VIII‘,‘IX‘];var arr10 = [‘‘,‘X‘,‘XX‘,‘XXX‘,‘XL‘,‘L‘,‘LX‘,‘LXX‘,‘LXXX‘,‘XC‘];var arr100 = [‘‘,‘C‘,‘CC‘,‘CCC‘,‘CD‘,‘D‘,‘DC‘,‘DCC‘,‘DCCC‘,‘CM‘];var arr1000 = [‘‘,‘M‘,‘MM‘,‘MMM‘];num = arr1000[Math.floor(num/1000)]+arr100[Math.floor(num%1000/100)]+arr10[Math.floor(num%100/10)]+arr1[Math.floor(num%10)];return num;}convert(36);
4. RELATED LINKS
- Http://www.mathsisfun.com/roman-numerals.html
- Http://www.runoob.com/jsref/jsref-floor.html
Roman Numeral Converter-freecodecamp Algorithm Topic