2018-10-04 14:37:37 +01:00
---
id: a7f4d8f2483413a6ce226cac
title: Roman Numeral Converter
isRequired: true
challengeType: 5
---
## Description
< section id = 'description' >
Convert the given number into a roman numeral.
All < a href = "http://www.mathsisfun.com/roman-numerals.html" target = "_blank" > roman numerals< / a > answers should be provided in upper-case.
Remember to use < a href = 'http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target = '_blank' > Read-Search-Ask< / a > if you get stuck. Try to pair program. Write your own code.
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: < code > convertToRoman(2)</ code > should return "II".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(2), "II");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(3)</ code > should return "III".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(3), "III");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(4)</ code > should return "IV".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(4), "IV");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(5)</ code > should return "V".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(5), "V");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(9)</ code > should return "IX".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(9), "IX");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(12)</ code > should return "XII".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(12), "XII");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(16)</ code > should return "XVI".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(16), "XVI");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(29)</ code > should return "XXIX".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(29), "XXIX");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(44)</ code > should return "XLIV".
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(44), "XLIV");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(45)</ code > should return "XLV"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(45), "XLV");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(68)</ code > should return "LXVIII"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(68), "LXVIII");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(83)</ code > should return "LXXXIII"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(83), "LXXXIII");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(97)</ code > should return "XCVII"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(97), "XCVII");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(99)</ code > should return "XCIX"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(99), "XCIX");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(400)</ code > should return "CD"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(400), "CD");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(500)</ code > should return "D"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(500), "D");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(501)</ code > should return "DI"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(501), "DI");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(649)</ code > should return "DCXLIX"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(649), "DCXLIX");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(798)</ code > should return "DCCXCVIII"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(798), "DCCXCVIII");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(891)</ code > should return "DCCCXCI"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(891), "DCCCXCI");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(1000)</ code > should return "M"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(1000), "M");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(1004)</ code > should return "MIV"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(1004), "MIV");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(1006)</ code > should return "MVI"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(1006), "MVI");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(1023)</ code > should return "MXXIII"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(1023), "MXXIII");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(2014)</ code > should return "MMXIV"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(2014), "MMXIV");
2018-10-04 14:37:37 +01:00
- text: < code > convertToRoman(3999)</ code > should return "MMMCMXCIX"
2019-07-24 01:56:38 -07:00
testString: assert.deepEqual(convertToRoman(3999), "MMMCMXCIX");
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function convertToRoman(num) {
return num;
}
convertToRoman(36);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function convertToRoman(num) {
var ref = [['M', 1000], ['CM', 900], ['D', 500], ['CD', 400], ['C', 100], ['XC', 90], ['L', 50], ['XL', 40], ['X', 10], ['IX', 9], ['V', 5], ['IV', 4], ['I', 1]];
var res = [];
ref.forEach(function(p) {
while (num >= p[1]) {
res.push(p[0]);
num -= p[1];
}
});
return res.join('');
}
```
< / section >