Leet Code – Integer to Roman Numeral Function in JavaScript

Algorithm

for decimal number X

Create a Object for each symbol as key and it’s respective
value

var roman = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,


This content originally appeared on DEV Community and was authored by Anuj Srivastav

Algorithm

for decimal number X

  • Create a Object for each symbol as key and it's respective value
  var roman = {
    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
  };
  • find the highest decimal value V that is less than or
    equal to the decimal number X.

  • Store the Roman numeral that you found after Subtracting
    it's value V from X
    X = X - V

  • Repeat above Process until you get zero of X

Example

X = 36

Image description

Javascript Function to Covert Integer to Roman Numeral



/**
 * @param {number} num
 * @return {string}
 */
var intToRoman = function(num,result=[]) {
  var roman = {
    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
  };
if(num == 0){
  return ''
}
for(const props in roman){
 if(roman[props]<=num){
  result.push(props);
  num = num - roman[props];
  intToRoman(num,result);
  return result.join("");
} 

}

};
console.log(intToRoman(36));

Test Cases :

Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.

Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.


This content originally appeared on DEV Community and was authored by Anuj Srivastav


Print Share Comment Cite Upload Translate Updates
APA

Anuj Srivastav | Sciencx (2022-07-23T12:12:18+00:00) Leet Code – Integer to Roman Numeral Function in JavaScript. Retrieved from https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/

MLA
" » Leet Code – Integer to Roman Numeral Function in JavaScript." Anuj Srivastav | Sciencx - Saturday July 23, 2022, https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/
HARVARD
Anuj Srivastav | Sciencx Saturday July 23, 2022 » Leet Code – Integer to Roman Numeral Function in JavaScript., viewed ,<https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/>
VANCOUVER
Anuj Srivastav | Sciencx - » Leet Code – Integer to Roman Numeral Function in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/
CHICAGO
" » Leet Code – Integer to Roman Numeral Function in JavaScript." Anuj Srivastav | Sciencx - Accessed . https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/
IEEE
" » Leet Code – Integer to Roman Numeral Function in JavaScript." Anuj Srivastav | Sciencx [Online]. Available: https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/. [Accessed: ]
rf:citation
» Leet Code – Integer to Roman Numeral Function in JavaScript | Anuj Srivastav | Sciencx | https://www.scien.cx/2022/07/23/leet-code-integer-to-roman-numeral-function-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.