i´m new to JavaScript and learning with the Help of the Website https://www.jshero.net/koans/roman1.html.
The exercise is to code a converter, that converts roman numbers from a string 'CDLXXXIII' to the arabic number.
I made a code with a "while loop" that works, but the website wants me to do it with a recursive function.
Heres my code:
function roman(roemische){
let romBuchstaben = ['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM', 'M'];
let romZahlen = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
let summe = 0;
while (roemische.length > 0){
let suchzeichen = roemische[0] + roemische[1];
if (romBuchstaben.indexOf(suchzeichen) !== -1){
summe += romZahlen[romBuchstaben.indexOf(suchzeichen)];
roemische = roemische.substr(2,roemische.length-2);
} else {
summe += romZahlen[romBuchstaben.indexOf(roemische[0])];
roemische = roemische.substr(1, roemische.length-1);
}
}
return summe;
}
(I´m sorry that the var's are in german).
I´m not that familiar with recursion, could some1 give me an example, how to do it with one?
Greetings Marcel