Dienstag, 25. August 2015

Roman numeral converter

As part of the "Difficulty 2" intermediate bonfires at Free Code Camp, the task was to create a Roman numeral converter. At first, I did not really have a clue what to do with this, I did a research anf dound pretty neat solutions, but they were either not complete, or too complicated for me to understand.

So I wrote my own code which does the following things:

1) Take the number, and convert it to an array

2) Loop through this array backwards and consider the different cases (is the number smaller than 5 but not equal to 4, is it equal to 4, is it greather than 5, but not equal to 9, or is it equal to nine? Then, of course, I had to consider whether the number was in the tenths, hundreds of thousands.

3) For each case, the corresponding roman numbers are written into a new, separate array.

4) In the end, the new array is reversed (because the numbers were written in opposite order) and joined to print the roman number without any spaces.

I have added a small form and some code to validate user input:


Keine Kommentare:

Kommentar veröffentlichen