SHARE
TWEET

Untitled

a guest May 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // roman numerals converter in JavaScript.
  2. const romanNumeral= n => {
  3.   let romanString = '';
  4.   let romanValues = [
  5.     ['M', 1000],
  6.     ['CM', 900],
  7.     ['D', 500],
  8.     ['CD', 400],
  9.     ['C', 100],
  10.     ['XC', 90],
  11.     ['L', 50],
  12.     ['XL', 40],
  13.     ['X', 10],
  14.     ['IX', 9],
  15.     ['V', 5],
  16.     ['IV', 4],
  17.     ['I', 1]
  18.   ];
  19.  
  20.   for (let r of romanValues) {
  21.     while (n >= r[1]) {
  22.       romanString += r[0]
  23.       n -= r[1];
  24.     }  
  25.   }
  26.  
  27.   return romanString;
  28. };
  29.  
  30. console.log(romanNumeral(350) === 'CCCL');
  31. console.log(romanNumeral(310) === 'CCCX');
  32.  
  33. console.log(romanNumeral(3) === 'III');
  34. console.log(romanNumeral(5) === 'V');
  35.  
  36. console.log(romanNumeral(2019) === 'MMXIX');
  37.  
  38. console.log(romanNumeral(2018) === 'MMXVIII');
  39. console.log(romanNumeral(54) === 'LIV');
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
 
Top