Hello fellow campers!
I’ve finished the challenge in question with the following code.
function convertToRoman(num) {
var romanLetters = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'];
var romanNumbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
var numToRoman = '';
for (var i = 0; i < romanLetters.length; i += 1){
while(num >= romanNumbers[i]){
numToRoman += romanLetters[i];
num -= romanNumbers[i];
}
}
return numToRoman;
}
convertToRoman(36);
The description hints about Array.prototype.splice(), Array.prototype.indexOf() and Array.prototype.join(). As you can see, I didn’t use any of these, so… I feel like my solution is not the ‘proper’ solution.
I really can’t figure out how to finish the challenge by using these methods, so any guidance will be appreciated.
As long as you solved it and you understood what you did, you’re doing fine. I didn’t use those as well . The hinted functions can be useful for solving the challenges, but by no means are they required.
Ok, to be honest I have no idea how to use those functions for this challenge
Our solutions are similar.
function convertToRoman(num) {
function values(roman, decimal) {
return {
roman: roman,
decimal: decimal
};
}
var table = [
values('M', 1000),
values('CM', 900),
values('D', 500),
values('CD', 400),
values('C', 100),
values('XC', 90),
values('L', 50),
values('XL', 40),
values('X', 10),
values('IX', 9),
values('V', 5),
values('IV', 4),
values('I', 1)
];
var n = num;
var roman = '';
var i = 0;
while (n > 0) {
if (n >= table[i].decimal) {
roman += table[i].roman;
n -= table[i].decimal;
}
else {
i++;
}
}
return roman;
}
I think, any method is ok if it works) I managed to use splice and join in mine solition, but have no clue where we could use indexOf