Roman Numeral Converter I'm stuck

No, it won’t work. The index in the new array is not the same in the numeral array. If I did that then it would give me M…etc because their indexes are 0 and the indexes from the new array start from 0.

That’s why I used IndexOf. To find the index of these numbers in the numbers array then transform them into numerals because the indexes are the same as the length/order is the same.

Really, there are no discussing with this.

Thank you all, again. The problem got solved.

I nothing say it is unique algo solution and I glad for you , but try pass number 98,99.

Again, I don’t understand what you’re trying to say.

I don’t mean insult but your english is not that good.

Also, floating numbers don’t exist in roman numerals so of course it won’t work. If you mean whole numbers, then they work correctly(98, 99), I don’t know what you want to mean.

I was thinking about passing 97 and 98, but today I try code and the output is good.
anyway I refactor your code

while (renum> 0 ){
    const EL = numbers.findIndex(element=> renum-element>=0);
    arr.push(numeral[[EL]]);
    renum-= numbers[EL];
    }
return arr.join('');
}

what I have on mind is the Principe

9,90,900 have only different alias but they have same position 9.
the I=1, X=10, C=100, M=1000, they have all same identification 1, its only matters how is number length.
the same is 5,50,500 V,L,D.
So I create matrix

Yeah. I updated the code with the same logic days ago. Thanks for the better code style though.

The issue has got solved a long time ago.

1 Like

here is what i come up with…

function convertToRoman(num) {
 let romanToNum = {
     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
 }
 let roman = "";
 for (let key in romanToNum) {
     while (num >= romanToNum[key]) {
         roman += key;
         num -= romanToNum[key];
     }
 }
 return roman;
}

convertToRoman(36);
1 Like