'I dont know why my loop adds one to many "X" to my result variable // freecodecamp romannumeralconverter

I want to convert any number that i put into the function to Roman numerals. It works for most numbers but when i try it with 44 it puts one X to many into my result variable.

Can anyone help me figure out why this happens?

   function convertToRoman(num) {

  const roman = {
    "M" : 1000,
    "CM" : 900,
    "D" : 500,
    "CD" : 400,
    "C" : 100,
    "XC" : 90,
    "L" : 50,
    "X" : 10,
    "IX" : 9,
    "V" : 5,
    "IV" : 4,
    "I" : 1,


// 36 -> 30 + 6
  }
  let result = "";
  console.log("num to convert: " + num)


  for (var key in roman){
    //console.log("key: " + key)
    //console.log("value: " + roman[key])
    while (num >= roman[key]){
      console.log(result)
      console.log(num)
      num -= roman[key];
      result += key;

    }
  }

console.log("result: " , result)

 return result;
}

convertToRoman(44);```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source