Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // roman numerals converter in JavaScript.
- const romanNumeral= n => {
- let romanString = '';
- let romanValues = [
- ['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]
- ];
- for (let r of romanValues) {
- while (n >= r[1]) {
- romanString += r[0]
- n -= r[1];
- }
- }
- return romanString;
- };
- console.log(romanNumeral(350) === 'CCCL');
- console.log(romanNumeral(310) === 'CCCX');
- console.log(romanNumeral(3) === 'III');
- console.log(romanNumeral(5) === 'V');
- console.log(romanNumeral(2019) === 'MMXIX');
- console.log(romanNumeral(2018) === 'MMXVIII');
- console.log(romanNumeral(54) === 'LIV');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement