Skip to content

Instantly share code, notes, and snippets.

@Ademking Ademking/README.md
Created Jun 24, 2019

Embed
What would you like to do?
Convert Numbers in Roman numerals
function toRoman(value) {
  let number = Math.ceil(parseInt(value));
  if (isNaN(number)) {
    console.log("The format is wrong! Please enter the number")
    return value
  } else {
    if (0 >= number) {
      console.log("Please enter a number greater than 0!")
      return value
    } else if (number >= 4000) {
      console.log("Please enter a number less than 3999!")
      return value
    } else {
      const romanNumber = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
      const decimalNumber = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
      let romanNumeral = ""
      for (let i = 0; i < romanNumber.length; i++) {
        while (number >= decimalNumber[i]) {
          number -= decimalNumber[i]
          romanNumeral += romanNumber[i]
        }
      }
      return romanNumeral
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.