--- title: Roman Numeral Converter --- ![:triangular_flag_on_post:](https://forum.freecodecamp.com/images/emoji/emoji_one/triangular_flag_on_post.png?v=3 ":triangular_flag_on_post:") Remember to use **`Read-Search-Ask`** if you get stuck. Try to pair program ![:busts_in_silhouette:](https://forum.freecodecamp.com/images/emoji/emoji_one/busts_in_silhouette.png?v=3 ":busts_in_silhouette:") and write your own code ![:pencil:](https://forum.freecodecamp.com/images/emoji/emoji_one/pencil.png?v=3 ":pencil:") ### ![:checkered_flag:](https://forum.freecodecamp.com/images/emoji/emoji_one/checkered_flag.png?v=3 ":checkered_flag:") Problem Explanation: You will create a program that converts an integer to a Roman Numeral. #### Relevant Links * Roman Numerals * Array.splice() * Array.indexOf() * Array.join() ## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 1 Creating two arrays, one with the Roman Numerals and one with the decimal equivalent for the new forms will be very helpful. > _try to solve the problem now_ ## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 2 If you add the numbers to the arrays that go before the new letter is introduced, like values for 4, 9, and 40, it will save you plenty of code. > _try to solve the problem now_ ## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 3 You can't have more than three consecutive Roman numerals together. > _try to solve the problem now_ ## Spoiler Alert! ![warning sign](//discourse-user-assets.s3.amazonaws.com/original/2X/2/2d6c412a50797771301e7ceabd554cef4edcd74d.gif) **Solution ahead!** ## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") Basic Code Solution: var convertToRoman = function(num) { var decimalValue = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]; var romanNumeral = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ]; var romanized = ''; for (var index = 0; index < decimalValue.length; index++) { while (decimalValue[index] <= num) { romanized += romanNumeral[index]; num -= decimalValue[index]; } } return romanized; } // test here convertToRoman(36); ### Code Explanation: * We start off by creating two arrays with default conversion with matching indices. These are called `decimalValue` and `romanNumeral`. We also create an empty string variable, `romanized`, which will house the final roman number. * Using a for loop, we loop through the indicies of the `decimalValue` array. We continue to loop until while the value at the current `index` will fit into `num`. * Next, we add the roman numeral and decrease `num` by the decimal equivalent. * Finally, we return the value of `romanized`. #### Relevant Links * For Loops * While Loops ## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution: function convertToRoman(num) { var romans = ["I", "V", "X", "L", "C", "D", "M"], ints = [], romanNumber = [], numeral = ""; while (num) { ints.push(num % 10); num = Math.floor(num/10); } for (i=0; iFor Loops * While Loops * Math ## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution: function convertToRoman(num) { var romans = [ // 10^i 10^i*5 ["I", "V"], // 10^0 ["X", "L"], // 10^1 ["C", "D"], // 10^2 ["M"] // 10^3 ], digits = num.toString() .split('') .reverse() .map(function(item, index) { return parseInt(item); }), numeral = ""; // Loop through each digit, starting with the ones place for (var i=0; iFor Loops * .split() * .reverse() * .map() * .toString() * parseInt() * .replace() * .repeat() ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: * ![:warning:](https://forum.freecodecamp.com/images/emoji/emoji_one/warning.png?v=3 ":warning:") **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution. * Add an explanation of your solution. * Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. ![:traffic_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/traffic_light.png?v=3 ":traffic_light:")