Hi, just a wonderin’ because I have a smug warm feeling at not using alot of reference to accomplish this challenge and I feel that that feeling may be misplaced as I have a habbit of producing very noob javascript!
So, is there a more concise way or a js.method() that a more experience developer would approach this challenge with? Does my logic take me round the houses or on this occasion is it straight to the point?
nb. The .map(Number) is the bit I referenced and in all honestly I don’t really understand what its doing there, just that it doesnt work without it. I know what .map is doing and I have seen Number() on mdn but it doesn’t really corelate to me yet, being inside the .map. If someone could explain what its doing, that’d be great.
Cheers
Mark
EDIT: I’ve worked out waht my (Number) is doing, or rather remembered… its to make the number that i’ve converted to a string and then split, back into number within the .map array.
numerals = {
1: 'I',
2: 'II',
3: 'III',
4: 'IV',
5: 'V',
6: 'VI',
7: 'VII',
8: 'VIII',
9: 'IX',
10: 'X',
20: 'XX',
30: 'XXX',
40: 'XL',
50: 'L',
60: 'LX',
70: 'LXX',
80: 'LXXX',
90: 'XC',
100: 'C',
200: 'CC',
300: 'CCC',
400: 'CD',
500: 'D',
600: 'DC',
700: 'DCC',
800: 'DCCC',
900:'CM',
1000: 'M',
2000: 'MM',
3000: 'MMM',
4000: 'MMMM',
5000: 'MMMMM'
};
function convertToRoman(num) {
var array = [];
num = num.toString().split("").map(Number);
num.reverse();
for (var i = 0; i < num.length; i++){
var multiplyer = Math.pow(10, [i]);
numeral = numerals[num[i]*multiplyer];
array.push(numeral);
}
converted = array.reverse().join('');
return converted;
}
convertToRoman(649);