-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |