Created
July 6, 2021 10:23
-
-
Save inter-coder/c7004484c8e4a6649e841c3340f6d544 to your computer and use it in GitHub Desktop.
convert integer to roman numeral javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(Number.prototype,'int2roman',{ | |
get: function(){ | |
const map = { | |
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, | |
}; | |
let result = ''; | |
let num=this.valueOf(); | |
for (key in map) { | |
result += key.repeat(Math.floor(num / map[key])); | |
num %= map[key]; | |
} | |
return result; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment