Roman Numeral Converter - error for forty

Roman Numeral Converter - error for forty
0

#1

Tell us what’s happening:
the following code works for all numbers except for a number containing 41 to 49. It works for 40.

I have attached the test case result. The test result says ’ Invalid count value’

Your code so far

function convertToRoman(num) {
  let roman = '';
  //thousands place
  if(num / 1000 > 0) {

    let m = Math.floor(num/1000);
    roman = roman + 'M'.repeat(m);
    num = num % 1000;

  } 

  //hundreds place
  if (Math.floor(num / 100) === 9) {

    roman = roman + 'CM';
    num = num % 100;

  } else if (num / 100 > 4) {

    let c = Math.floor(num/100) - 5;
    roman = roman + 'D' + 'C'.repeat(c);
    num = num % 100;

  } else if (Math.floor(num / 100) === 4) {

    roman = roman + 'CD';
    num = num % 100;

  } else if (num / 100 > 0) {

    let c = Math.floor(num/100);
    roman = roman + 'C'.repeat(c);
    num = num % 100;

  }

  if (Math.floor(num / 10) === 9) {

    roman = roman + 'XC';
    num = num % 10;

  } else if (num / 10 > 4) {

    let c = Math.floor(num/10) - 5;
    roman = roman + 'L' + 'X'.repeat(c);
    num = num % 10;

  } else if (Math.floor(num / 10) === 4) {

    roman = roman + 'XL';
    num = num % 10;
    
  } else if (num / 10 > 0) {

      let c = Math.floor(num/10);
      roman = roman + 'X'.repeat(c);
      num = num % 10;
    
  } 

  if (Math.floor(num % 10) === 9) {

    roman = roman + 'IX';
    return roman;

  } else if (num % 10 > 4) {

    let i = Math.floor(num%10) - 5;
    roman = roman + 'V' + 'I'.repeat(i);
    return roman;

  } else if (Math.floor(num % 10) === 4) {

    roman = roman + 'IV';
    return roman;

  } else if (num % 10 > 0) {

    let i = Math.floor(num%10);
    roman = roman + 'I'.repeat(i);

  }
  return roman;
}

convertToRoman(36);
![16|368x218](upload://xEg8MjEZE7QivtNXhjNqTaCiCHd.png) ![01|265x500](upload://mCKyuzsdR55GI3HPUlTb9F6uvAC.png) 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/roman-numeral-converter


#2

When calling convertToRoman(44), the following section causes an error, because c is -1 and you can not use a negative number for the repeat function.

  } else if (num / 10 > 4) {
    let c = Math.floor(num/10) - 5;
    roman = roman + 'L' + 'X'.repeat(c);
    num = num % 10;
  }

#3

Thanks for the timely response. I have one more question, 44 / 10 > 4 is false. So should the following segment be the one to handle 44?

else if (Math.floor(num / 10) === 4) {

    roman = roman + 'XL';
    num = num % 10;
    
  }

#4

44 / 10 > 4 is true not false.