Skip to content

Instantly share code, notes, and snippets.

@inter-coder
Created July 6, 2021 10:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save inter-coder/c7004484c8e4a6649e841c3340f6d544 to your computer and use it in GitHub Desktop.
convert integer to roman numeral javascript
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