Better Roman Numeral

Tell us what’s happening:
Describe your issue in detail here.

I wanted to see a more efficient solution and one that actually utilized the “forming” rule: where if the symbol appears before a larger symbol its subtracted (example: IV = V − I = 5 − 1 = 4).

   **Your code so far**

function convertToRoman(num) {
 const numerals = {
 let split = splitNumber(num);
 let numArr = Object.keys(numerals).sort((a,b)=> a+b);
 let str = '';
 let str2=[]
 //start looping
 for (let i = 0; i < split.length; i++){
   let r = split[i];
   for(let j = 0; j < numArr.length ; j++){
     while(r >= numArr[j]){
       r -= numArr[j];   
       str = str.concat(numerals[numArr[j]]);
return str;

const splitNumber = (num, arr = [], m = 1) => {
     return splitNumber(Math.floor(num / 10), [m * (num % 10)].concat(arr), m * 10);
  return arr;

   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0

Challenge: Roman Numeral Converter

Link to the challenge:

HI @TTime !

I originally edited your code with [spoiler] tags because you said this was a working solution.
But it is not passing a lot of the tests.

So, my code works on firefox but not on brave browser, I found out the issue was my:

let numArr = Object.keys(numerals).sort((a,b)=> a+b);

isnt sorting the object in descending order on Brave , but it works on Firefox.

It shouldn’t working either browser. Sort by default sorts the values as if they are strings. woops, that part isn’t the issue. The sort function is bad.

It should totally work cause I am using it to return a string.

That makes no sense.

You are intending to make an array of strings out of numbers by sorting on their sum? Why?!?!

The typical way to compare numbers is with their difference.

Ah, see what is happening. Key order isn’t specified for any implementation. You are accidentally doing no sort, just a reverse of whatever order is present, and it just so happens that in some browsers the keys are already in the right order for this to work.

:D, exactly I needed it in descending order , by default it does it in ascending order, I should have specified that in the beginning.

By default it does not do numerically ascending order. It does lexographic ascending order.

To use numerically ascending, use the sort function in the docs. For numerically descending, multiply that value by -1 (flip the order in the operation used).

<:O , that makes sense, in the beginning I just assumed that the object wouldn’t do any numerical or lexographic sorting and just place the key-pair values in the same fashion that I wrote it out as.

Yeah, that part is what I would expect too, but internally browsers can re order for reasons according to the specs.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.