Skip to main content

--description--

Convert the given number into a roman numeral.

Roman numeralsArabic numerals
M1000
CM900
D500
CD400
C100
XC90
L50
XL40
X10
IX9
V5
IV4
I1

All roman numerals answers should be provided in upper-case.

--hints--

convertToRoman(2) should return the string II.

assert.deepEqual(convertToRoman(2), 'II');

convertToRoman(3) should return the string III.

assert.deepEqual(convertToRoman(3), 'III');

convertToRoman(4) should return the string IV.

assert.deepEqual(convertToRoman(4), 'IV');

convertToRoman(5) should return the string V.

assert.deepEqual(convertToRoman(5), 'V');

convertToRoman(9) should return the string IX.

assert.deepEqual(convertToRoman(9), 'IX');

convertToRoman(12) should return the string XII.

assert.deepEqual(convertToRoman(12), 'XII');

convertToRoman(16) should return the string XVI.

assert.deepEqual(convertToRoman(16), 'XVI');

convertToRoman(29) should return the string XXIX.

assert.deepEqual(convertToRoman(29), 'XXIX');

convertToRoman(44) should return the string XLIV.

assert.deepEqual(convertToRoman(44), 'XLIV');

convertToRoman(45) should return the string XLV.

assert.deepEqual(convertToRoman(45), 'XLV');

convertToRoman(68) should return the string LXVIII

assert.deepEqual(convertToRoman(68), 'LXVIII');

convertToRoman(83) should return the string LXXXIII

assert.deepEqual(convertToRoman(83), 'LXXXIII');

convertToRoman(97) should return the string XCVII

assert.deepEqual(convertToRoman(97), 'XCVII');

convertToRoman(99) should return the string XCIX

assert.deepEqual(convertToRoman(99), 'XCIX');

convertToRoman(400) should return the string CD

assert.deepEqual(convertToRoman(400), 'CD');

convertToRoman(500) should return the string D

assert.deepEqual(convertToRoman(500), 'D');

convertToRoman(501) should return the string DI

assert.deepEqual(convertToRoman(501), 'DI');

convertToRoman(649) should return the string DCXLIX

assert.deepEqual(convertToRoman(649), 'DCXLIX');

convertToRoman(798) should return the string DCCXCVIII

assert.deepEqual(convertToRoman(798), 'DCCXCVIII');

convertToRoman(891) should return the string DCCCXCI

assert.deepEqual(convertToRoman(891), 'DCCCXCI');

convertToRoman(1000) should return the string M

assert.deepEqual(convertToRoman(1000), 'M');

convertToRoman(1004) should return the string MIV

assert.deepEqual(convertToRoman(1004), 'MIV');

convertToRoman(1006) should return the string MVI

assert.deepEqual(convertToRoman(1006), 'MVI');

convertToRoman(1023) should return the string MXXIII

assert.deepEqual(convertToRoman(1023), 'MXXIII');

convertToRoman(2014) should return the string MMXIV

assert.deepEqual(convertToRoman(2014), 'MMXIV');

convertToRoman(3999) should return the string MMMCMXCIX

assert.deepEqual(convertToRoman(3999), 'MMMCMXCIX');

--seed--

--seed-contents--

function convertToRoman(num) {
return num;
}

convertToRoman(36);

--solutions--

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('');
}