Advertisement
Guest User

arabicToRoman.js

a guest
Jul 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arabicToRoman(n) {
  2.     if (!Number.isInteger(n) || n <= 0 || n >= 4000) {
  3.         throw new TypeError("The argument must be a natural number less than 4000!");
  4.     }
  5.  
  6.     const digits = [[], [0], [0, 0], [0, 0, 0], [0, 1], [1], [1, 0], [1, 0, 0], [1, 0, 0, 0], [0, 2]];
  7.     const letters = ['IVX', 'XLC', 'CDM', 'M'];
  8.     let result = '';
  9.     for (let i = 0; n > 0; i++) {
  10.         result = digits[n % 10].map(x => letters[i][x]).join('') + result;
  11.         n = Math.floor(n / 10);
  12.     }
  13.  
  14.     return result;
  15. }
  16.  
  17. module.exports = arabicToRoman;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement