# Roman Numeral Converter Help

Hey there, I am having trouble with this challenge. All I have to do is convert a number into a roman numeral. Right now I am trying to break the challenge down and do it in smaller chunks, and move my way up to be able to convert larger numbers. I am able to convert numbers 1-10, but as soon as I get above 10 my output gets funky, and I am not sure why. I would appreciate any feedback on why my loop isn’t correctly outputting numbers larger than 10.

``````function convertToRoman(num) {
var decimalValue = [1, 4, 5, 9, 10], //40, 50, 90, 100, 400, 500, 900, 1000],
romanNumeral = ['I', 'IV', 'V', 'IX', 'X'], //'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'],
romanized = '',
newArr = [],
reverseNum = num.toString().split('').reverse().join('');

for (i = 0; i < reverseNum.length; i ++) {
var power = Math.pow(10, i);
var newNumb = power * reverseNum[i];
var zero = 0;
for (j = 0; j < decimalValue.length; j ++) {
var subtract = 0;
if (newNumb == decimalValue[j]) {
romanized += romanNumeral[j];

} else if (reverseNum[i] === '0') {
zero += 1;
}

else if (newNumb < decimalValue[j]) {
subtract = reverseNum - decimalValue[j - 1];
romanized = romanNumeral[j - 1];
for (k = 0; k < subtract; k ++) {
romanized += romanNumeral;
}
break;
}
}
}

return romanized;

}

convertToRoman(11);

``````

You don’t need any Math operations for this.

Haha this challenge is whooping me. This was the best I could come up with I don’t know what else to do.

Don’t worry. I think I did a similar thing once in a different language.
Anyway, the method for doing this is to start at the right with the highest value digit and work back down to the lowest digit

By starting with the highest value digit, do you mean starting with, let’s say the 100s position in a number like 142, and moving down to the ones position? Or, do you mean starting with the 10s position since it is the highest value?

1 Like

Correct. Have 3 arrays representing the different roman units to make things easier. (0s, 10s, 100s)
(You would need more for bigger value numbers of course)

I’ve just had a go at it myself and these are the variables I use:

``````  var decimalValue = [1, 4, 5, 9, 10],
tensValue = [10,40,50,90],
hundredsValue = [100,400,500,900,1000],
romanNumeral = ['I', 'IV', 'V', 'IX', 'X'],
romanTens = ['X', 'XL', 'L', 'XC'],
roman100s = ['C', 'CD', 'D', 'CM', 'M'],
romanized = '';
``````

I’m still stumped. I can’t think of any other way to do it other than what I have already worked out. I don’t know how to use a for loop to check each digit against a different array depending on what loop it is on. For Example, if I am looping through my input, 123, I don’t know how to check 1 on my first loop through with hundredsValue array, and 2 with the tensValue Array on the next loop. The only thing I can think of is to take the code that I have already written that works for values 1-10, and to repeat for however many digits I have

i did it , with very weird style of programming, using simple if else statement

I got an answer. This is probably more complicated than it needed to be:

``````
function convertToRoman(num) {
var number = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ],
roman = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ],
newArr = [],
newNum = num.toString().split('').reverse(),
subtract = 0;

if (number.indexOf(num) > -1) {
newArr.unshift(roman[number.indexOf(num)]);
} else {
for (a = 0; a < newNum.length; a ++) {
if (newNum[a] < 4) {
for (i = 0; i < newNum[a]; i ++) {
newArr.unshift(roman[number.indexOf(number * Math.pow(10, a))]);
}
} else if (newNum[a] > 5 && newNum[a] < 9) {
subtract = newNum[a] - 5;
for (j = 0; j < subtract; j ++) {
newArr.unshift(roman[number.indexOf(number * Math.pow(10, a))]);
}
newArr.unshift(roman[number.indexOf(number * Math.pow(10, a))]);
} else if (number.indexOf(parseInt(newNum[a])) > -1) {
newArr.unshift(roman[number.indexOf(parseInt(newNum[a]) * Math.pow(10, a))]);
} else {
if (newNum[a] === 0) {
newArr.unshift('');
}
}
}
}

return newArr.join('');

}

convertToRoman(44);
``````