javascript - roman-numerals

This commit is contained in:
James Walker 2018-09-26 12:26:28 -04:00
parent 0282389eb4
commit e8af849460
Signed by: walkah
GPG Key ID: 3C127179D6086E93
5 changed files with 7276 additions and 0 deletions

View File

@ -0,0 +1,79 @@
# Roman Numerals
Write a function to convert from normal numbers to Roman Numerals.
The Romans were a clever bunch. They conquered most of Europe and ruled
it for hundreds of years. They invented concrete and straight roads and
even bikinis. One thing they never discovered though was the number
zero. This made writing and dating extensive histories of their exploits
slightly more challenging, but the system of numbers they came up with
is still in use today. For example the BBC uses Roman numerals to date
their programmes.
The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice
these letters have lots of straight lines and are hence easy to hack
into stone tablets).
```text
1 => I
10 => X
7 => VII
```
There is no need to be able to convert numbers larger than about 3000.
(The Romans themselves didn't tend to go any higher)
Wikipedia says: Modern Roman numerals ... are written by expressing each
digit separately starting with the left most digit and skipping any
digit with a value of zero.
To see this in practice, consider the example of 1990.
In Roman numerals 1990 is MCMXC:
1000=M
900=CM
90=XC
2008 is written as MMVIII:
2000=MM
8=VIII
See also: http://www.novaroma.org/via_romana/numbers.html
## Setup
Go through the setup instructions for Javascript to
install the necessary dependencies:
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
## Requirements
Install assignment dependencies:
```bash
$ npm install
```
## Making the test suite pass
Execute the tests with:
```bash
$ npm test
```
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by
changing `xtest` to `test`.
## Source
The Roman Numeral Kata [http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals](http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

7080
javascript/roman-numerals/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
{
"name": "exercism-javascript",
"version": "0.0.0",
"description": "Exercism exercises in Javascript.",
"author": "Katrina Owen",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript"
},
"devDependencies": {
"babel-jest": "^21.2.0",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-preset-env": "^1.4.0",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^5.0.1",
"eslint-plugin-react": "^7.0.1",
"jest": "^21.2.1"
},
"jest": {
"modulePathIgnorePatterns": [
"package.json"
]
},
"babel": {
"presets": [
[
"env",
{
"targets": [
{
"node": "current"
}
]
}
]
],
"plugins": [
[
"babel-plugin-transform-builtin-extend",
{
"globals": [
"Error"
]
}
],
[
"transform-regenerator"
]
]
},
"scripts": {
"test": "jest --no-cache ./*",
"watch": "jest --no-cache --watch ./*",
"lint": "eslint .",
"lint-test": "eslint . && jest --no-cache ./* "
},
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": "airbnb",
"rules": {
"import/no-unresolved": "off",
"import/extensions": "off"
}
},
"licenses": [
"MIT"
],
"dependencies": {}
}

View File

@ -0,0 +1,15 @@
const toRoman = function (number) {
const numerals = { 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 (const n in numerals) {
const value = Math.floor(number / numerals[n]);
if (value > 0) {
roman += n.repeat(value);
number -= (value * numerals[n]);
}
}
return roman;
};
export default toRoman;

View File

@ -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'));
});