SHARE
TWEET

Untitled

a guest Sep 16th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function toRoman(num) {
  2.   let result = '';
  3.   const decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  4.   const roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
  5.   for (let i = 0;i<=decimal.length;i++) {
  6.     while (num%decimal[i] < num) {    
  7.       result += roman[i];
  8.       num -= decimal[i];
  9.     }
  10.   }
  11.   return result;
  12. }
  13.  
  14. toRoman(123); //returns "CXXIII"
  15. toRoman(3290); // returns "MMMCCXC"
  16. toRoman(777); // returns "DCCLXXVII"
  17. toRoman(949); // returns "CMXLIX"
  18. toRoman(2000); // returns "MM"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top