Skip to content

Instantly share code, notes, and snippets.

@martinsson
Last active February 8, 2018 23:36
Show Gist options
  • Save martinsson/ef0a90fe7c3ed37d2bc0c6ecd077caaa to your computer and use it in GitHub Desktop.
Save martinsson/ef0a90fe7c3ed37d2bc0c6ecd077caaa to your computer and use it in GitHub Desktop.
Example of a solution to the Roman Numerals Kata, done Craftsmanship Grenoble
import {assert} from "chai";
import {Roman} from "../src/Roman";
function toRoman(arabic: number) {
return new Roman().toRoman(arabic);
}
describe('RomanNumbers', () => {
describe('toRoman() is', () => {
function assertRomanIs(arabic: number, expected: string) {
assert.equal(toRoman(arabic), expected);
}
it('I for 1', () => {
assertRomanIs(1, 'I');
});
it('II for 2', () => {
assertRomanIs(2, 'II');
});
it('is III for 3', () => {
assertRomanIs(3, 'III');
});
it('is IV for 4', () => {
assertRomanIs(4, 'IV');
});
it('adds V for numbers between 8 and 5', () => {
assertRomanIs(5, 'V')
assertRomanIs(6, 'VI');
assertRomanIs(7, 'VII');
assertRomanIs(8, 'VIII');
});
it('is IX for 9', () => {
assertRomanIs(9, 'IX');
});
it('adds X for numbers between 19 and 10', () => {
assertRomanIs(10, 'X');
assertRomanIs(11, 'XI');
// ...
});
});
});
export class Roman {
public toRoman(numberToConvert: number) {
let romanDigits: [number, string][] = [
[10, 'X'],
[9, 'IX'],
[5, 'V'],
[4, 'IV'],
[1, 'I'],
];
let romanNumber = "";
romanDigits.forEach(([arabic, romanDigit]) => {
while (numberToConvert >= arabic) {
romanNumber += romanDigit;
numberToConvert -= arabic;
}
});
return romanNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment