Skip to content

Commit

Permalink
Added converter to roman numerals
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabinka committed Jan 8, 2019
1 parent aae97db commit 8f54e52
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
88 changes: 88 additions & 0 deletions javascript/package.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
36 changes: 36 additions & 0 deletions javascript/roman-numerals/roman-numerals.js
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 22 additions & 0 deletions javascript/roman-numerals/roman-numerals.spec.js
Original file line number Diff line number Diff line change
@@ -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'));
});

0 comments on commit 8f54e52

Please sign in to comment.