Skip to content

Commit

Permalink
Changed; convertNumerals to convert numbers up to 11
Browse files Browse the repository at this point in the history
I realised I essentially could achieve what I have coded more effectively in a loop.
  • Loading branch information
kirgy committed Feb 3, 2021
1 parent 43b0bc0 commit ee87430
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/numerals.ts
@@ -1,22 +1,22 @@
interface intRomDef {
int: number,
rom: string,
}

export const convertNumerals = (givenInt: number) : string => {
let romanNumeral: string = '';
let remainValue: number = givenInt;

if (remainValue == 4) {
romanNumeral += 'IV'
remainValue -= 4;
}

if (remainValue >= 5) {
romanNumeral += 'V'
remainValue -= 5;
}


while (remainValue > 0) {
romanNumeral += 'I';
remainValue -= 1;
}
const intToRom: Array<intRomDef> = [
{int:10, rom:'X'}, {int:9, rom:'IX'}, {int:5, rom:'V'}, {int:4, rom:'IV'}, {int:1, rom:'I'},
];

intToRom.forEach(intRom => {
while (remainValue > intRom.int-1) {
romanNumeral += intRom.rom;
remainValue -= intRom.int;
}
});

return romanNumeral;
}

0 comments on commit ee87430

Please sign in to comment.