From 2e127382cbadef5cdf45683ed72c8e604959d570 Mon Sep 17 00:00:00 2001 From: Aerex Date: Sat, 11 Jan 2020 00:27:25 -0500 Subject: [PATCH] feat: Added js implementation for integer-to-roman --- integer-to-roman/nodejs/index.js | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 integer-to-roman/nodejs/index.js diff --git a/integer-to-roman/nodejs/index.js b/integer-to-roman/nodejs/index.js new file mode 100644 index 0000000..94039ba --- /dev/null +++ b/integer-to-roman/nodejs/index.js @@ -0,0 +1,115 @@ +/* +Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. + +Symbol Value +I 1 +V 5 +X 10 +L 50 +C 100 +D 500 +M 1000 +For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. + +Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: + +I can be placed before V (5) and X (10) to make 4 and 9. +X can be placed before L (50) and C (100) to make 40 and 90. +C can be placed before D (500) and M (1000) to make 400 and 900. +Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. +*/ + +const { assert } = require('../../util/js'); + +/** + * @param {number} num + * @return {string} + */ +const intToRoman = function(num) { + const romanMap = { + "1": "I", + "4": "IV", + "5": "V", + "9": "IX", + "10": "X", + "40": "XL", + "50": "L", + "90": "XC", + "100": "C", + "400": "CD", + "500": "D", + "900": "CM", + "1000": "M" + }; + const numberOfDigits = Number(num).toString().length; + const pows = []; + + // Create an array representing the powers of 10s of the number + let remaining = num; + for (let i = 0; i < numberOfDigits; i++){ + let digit = (remaining / (Math.pow(10, i))) % 10; + remaining = remaining - (Math.pow(10, i) *digit); + pows.unshift(Math.pow(10,i)*digit); + } + const result = []; + const counters = [9, 5, 4, 1]; + // Check hash map if it doesn't exist subtract the largest valid roman numeral and save valid numeral + pows.forEach(pow => { + if (romanMap[pow]){ + result.push(romanMap[pow]); + } else { + let rem = pow; + + while(rem) { + const numberOfDigits = Number(rem).toString().length; + counters.forEach(counter => { + const key = counter * Math.pow(10, numberOfDigits-1); + if (key <= rem) { + rem = rem - key; + result.push(romanMap[key]); + } + }); + } + } + }) + return result.join(''); +}; +/* Test Case 1 + * Input: 3 + * Output: "III" + */ +x = 3; +sol = 'III'; +assert(intToRoman(x) === sol, 'Output: III'); + +/* Test Case 2 + * Input: 4 + * Output: IV + */ +x = 4; +sol = 'IV'; +assert(intToRoman(x) === sol, 'Output: IV'); + +/* Test Case 3 + * Input: 9 + * Output: 'IX' + */ +x = 9; +sol = 'IX'; +assert(intToRoman(x) == sol, 'Output: IX'); + +/* Test Case 4 + * Input: 58 + * Output: 'LVIII' + */ +x = 58; +sol = 'LVIII'; +assert(intToRoman(x) == sol, 'Output: LVIII'); + +/* Test Case 5 + * Input: 1994 + * Output: 'MCMXCIV' + */ +x = 1994; +sol = 'MCMXCIV'; +assert(intToRoman(x) == sol, 'Output: MCMXCIV');