Okay I wasted two good days trying to make it simple but anyway ended with this huge enormous beast. In the middle of the process I even decided to make it some visual output for easier representation.
http://codepen.io/Zeronsul/full/ORLYxw/
Im wondering how do you people solve this?
Previously:
Holly molly, you guys are monsters!
1 Like
Changed It to fit FCC format (removed CHSS and HTML sections and put everything under function tab)
https://jsfiddle.net/kyuynttL/
(jeez my brain is trembling!)
I didnt change anything else except provided switch for various array.length. Despite all other people marvellous and elegant solutions I didnt quite understand the math behind them yet so used my previous solution, hence the ‘flavor’ lol.
I never look at other people’s solutions unless I want to feel bad about myself
4 Likes
I also had “dict” in my solution))
function convertToRoman(num) {
var arr = (num+"").split("");
var multi = 1;
for(var i=arr.length-1; i>=0; i--){
arr[i] = arr[i] * multi;
multi *= 10;
}
var dict = { 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" };
for (var j=0; j<arr.length;j++){
arr.splice(j, 1, dict[arr[j]]);
}
num = arr.join("");
return num;
}