From 8f54e525b24fe00c41542cec0be6d4754030346c Mon Sep 17 00:00:00 2001 From: zhabinka Date: Tue, 8 Jan 2019 16:21:09 +0300 Subject: [PATCH] Added converter to roman numerals --- .gitignore | 12 +++ javascript/package.json | 88 +++++++++++++++++++ javascript/roman-numerals/roman-numerals.js | 36 ++++++++ .../roman-numerals/roman-numerals.spec.js | 22 +++++ 4 files changed, 158 insertions(+) create mode 100644 .gitignore create mode 100644 javascript/package.json create mode 100644 javascript/roman-numerals/roman-numerals.js create mode 100644 javascript/roman-numerals/roman-numerals.spec.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a4a0b9b --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +javascript/node_modules/ +javascript/package-lock.json + +javascript/*/README.md +javascript/*/package.json +*.exercism + +javascript/hello-world +javascript/darts +javascript/leap +javascript/two-fer +javascript/reverse-string \ No newline at end of file diff --git a/javascript/package.json b/javascript/package.json new file mode 100644 index 0000000..38a89c3 --- /dev/null +++ b/javascript/package.json @@ -0,0 +1,88 @@ +{ + "name": "exercism-javascript", + "version": "0.0.1", + "description": "Exercism exercises in Javascript.", + "author": "Katrina Owen", + "private": true, + "repository": { + "type": "git", + "url": "https://github.com/exercism/javascript" + }, + "devDependencies": { + "@babel/cli": "^7.1.0", + "@babel/core": "^7.1.0", + "@babel/node": "^7.0.0", + "@babel/preset-env": "^7.1.0", + "babel-core": "^7.0.0-0", + "babel-eslint": "^10.0.1", + "babel-jest": "^21.2.0", + "babel-plugin-transform-builtin-extend": "^1.1.2", + "babel-preset-env": "^1.7.0", + "eslint": "^5.6.0", + "eslint-config-airbnb-base": "^13.1.0", + "eslint-plugin-import": "^2.14.0", + "jest": "^23.6.0", + "cross-env": "^5.2.0" + }, + "jest": { + "modulePathIgnorePatterns": [ + "package.json" + ] + }, + "babel": { + "presets": [ + [ + "env", + { + "targets": [ + { + "node": "current" + } + ] + } + ] + ], + "plugins": [ + [ + "babel-plugin-transform-builtin-extend", + { + "globals": [ + "Error" + ] + } + ], + [ + "transform-regenerator" + ] + ] + }, + "preferGlobal": true, + "scripts": { + "test": "jest --no-cache ./*", + "watch": "jest --no-cache --watch ./*", + "lint": "eslint .", + "lint-test": "eslint . && jest --no-cache ./* ", + "babel-node": "babel-node" + }, + "eslintConfig": { + "parser": "babel-eslint", + "parserOptions": { + "ecmaVersion": 7, + "sourceType": "module" + }, + "env": { + "es6": true, + "node": true, + "jest": true + }, + "extends": "airbnb-base", + "rules": { + "import/no-unresolved": "off", + "import/extensions": "off", + "import/prefer-default-export": "off", + "import/no-default-export": "off" + } + }, + "license": "MIT", + "dependencies": {} +} diff --git a/javascript/roman-numerals/roman-numerals.js b/javascript/roman-numerals/roman-numerals.js new file mode 100644 index 0000000..332be3a --- /dev/null +++ b/javascript/roman-numerals/roman-numerals.js @@ -0,0 +1,36 @@ +const romanCodes = { + 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, +}; + +const getKeyByValue = (obj, value) => Object.keys(obj).find(key => obj[key] === value); + +const toRoman = (num) => { + const iter = (n, roman, i) => { + if (n === 0) { + return roman; + } + + const currentArabic = Object.values(romanCodes)[i]; + const currentRoman = getKeyByValue(romanCodes, currentArabic); + + const resault = roman.concat(currentRoman.repeat(Math.floor(n / currentArabic))); + + return iter(n % currentArabic, resault, i + 1); + }; + + return iter(num, '', 0); +}; + +export { toRoman }; diff --git a/javascript/roman-numerals/roman-numerals.spec.js b/javascript/roman-numerals/roman-numerals.spec.js new file mode 100644 index 0000000..42dce59 --- /dev/null +++ b/javascript/roman-numerals/roman-numerals.spec.js @@ -0,0 +1,22 @@ +import { toRoman } from './roman-numerals'; + +describe('toRoman()', () => { + test('converts 1', () => expect(toRoman(1)).toEqual('I')); + test('converts 2', () => expect(toRoman(2)).toEqual('II')); + test('converts 3', () => expect(toRoman(3)).toEqual('III')); + test('converts 4', () => expect(toRoman(4)).toEqual('IV')); + test('converts 5', () => expect(toRoman(5)).toEqual('V')); + test('converts 6', () => expect(toRoman(6)).toEqual('VI')); + test('converts 9', () => expect(toRoman(9)).toEqual('IX')); + test('converts 27', () => expect(toRoman(27)).toEqual('XXVII')); + test('converts 48', () => expect(toRoman(48)).toEqual('XLVIII')); + test('converts 59', () => expect(toRoman(59)).toEqual('LIX')); + test('converts 93', () => expect(toRoman(93)).toEqual('XCIII')); + test('converts 141', () => expect(toRoman(141)).toEqual('CXLI')); + test('converts 163', () => expect(toRoman(163)).toEqual('CLXIII')); + test('converts 402', () => expect(toRoman(402)).toEqual('CDII')); + test('converts 575', () => expect(toRoman(575)).toEqual('DLXXV')); + test('converts 911', () => expect(toRoman(911)).toEqual('CMXI')); + test('converts 1024', () => expect(toRoman(1024)).toEqual('MXXIV')); + test('converts 3000', () => expect(toRoman(3000)).toEqual('MMM')); +});