From fbda8c206bdeae8c03ad052bbf034c61670701e6 Mon Sep 17 00:00:00 2001 From: jaymovaliya Date: Tue, 6 Oct 2020 17:33:13 +0530 Subject: [PATCH 1/2] added code for converting integer to roman representation of number --- snippets/intToRoman.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 snippets/intToRoman.md diff --git a/snippets/intToRoman.md b/snippets/intToRoman.md new file mode 100644 index 000000000..7d4181726 --- /dev/null +++ b/snippets/intToRoman.md @@ -0,0 +1,31 @@ +--- +title: intToRoman +tags: integer,roman +--- + +### Convert Integer to Roman Number + +Following snippet is useful for converting integer number into roman number. + +***Note:-*** This code is only useful for number between 1 and 3999. +according to Wikipedia the largest number you can represent in Roman is 3999. [click here](https://en.wikipedia.org/wiki/Roman_numerals) + +```js +function intToRoman(num) { + const lookup = {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 roman = ''; + for (let i in lookup ) { + while ( num >= lookup[i] ) { + roman += i; + num -= lookup[i]; + } + } + return roman; +} +``` + +```js +intToRoman(11); // XI +intToRoman(03); // III +intToRoman(1998) // MCMXCVIII +``` From 94cff00712ea9de0fe4a422e5b5c870266d97f10 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Tue, 6 Oct 2020 19:56:22 +0300 Subject: [PATCH 2/2] Update and rename intToRoman.md to toRomanNumeral.md --- snippets/intToRoman.md | 31 ---------------------------- snippets/toRomanNumeral.md | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 31 deletions(-) delete mode 100644 snippets/intToRoman.md create mode 100644 snippets/toRomanNumeral.md diff --git a/snippets/intToRoman.md b/snippets/intToRoman.md deleted file mode 100644 index 7d4181726..000000000 --- a/snippets/intToRoman.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: intToRoman -tags: integer,roman ---- - -### Convert Integer to Roman Number - -Following snippet is useful for converting integer number into roman number. - -***Note:-*** This code is only useful for number between 1 and 3999. -according to Wikipedia the largest number you can represent in Roman is 3999. [click here](https://en.wikipedia.org/wiki/Roman_numerals) - -```js -function intToRoman(num) { - const lookup = {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 roman = ''; - for (let i in lookup ) { - while ( num >= lookup[i] ) { - roman += i; - num -= lookup[i]; - } - } - return roman; -} -``` - -```js -intToRoman(11); // XI -intToRoman(03); // III -intToRoman(1998) // MCMXCVIII -``` diff --git a/snippets/toRomanNumeral.md b/snippets/toRomanNumeral.md new file mode 100644 index 000000000..298700856 --- /dev/null +++ b/snippets/toRomanNumeral.md @@ -0,0 +1,42 @@ +--- +title: toRomanNumeral +tags: math,string,intermediate +--- + +Converts an integer to its roman numeral representation. +Accepts value between `1` and `3999` (both inclusive). + +- Create a lookup table containing 2-value arrays in the form of (roman value, integer). +- Use `Array.prototype.reduce()` to loop over the values in `lookup` and repeatedly divide `num` by the value, using `String.prototype.repeat()` to add the roman numeral representation to the accumulator. + +```js +const toRomanNumeral = num => { + const lookup = [ + ['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], + ]; + return lookup.reduce((acc, [k, v]) => { + acc += k.repeat(Math.floor(num / v)); + num = num % v; + return acc; + }, ''); +}; + +``` + +```js +toRomanNumeral(3); // 'III' +toRomanNumeral(11); // 'XI' +toRomanNumeral(1998); // 'MCMXCVIII' +```