# Roman numeral converter - recursion problem

**URL:** <https://forum.freecodecamp.org/t/roman-numeral-converter-recursion-problem/157823>\
**Category:** General\
**Created:** [November 7, 2017, 4:37pm UTC](https://forum.freecodecamp.org/t/roman-numeral-converter-recursion-problem/157823 "2017-11-07T16:37:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Strzesia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/strzesia/32/67236_2.png) [@Strzesia](https://forum.freecodecamp.org/u/Strzesia)\
**Post date:** [November 7, 2017, 4:37pm UTC](https://forum.freecodecamp.org/t/roman-numeral-converter-recursion-problem/157823/1 "2017-11-07T16:37:58Z")

</div>

Hi guys.  
I tried to write roman numeral converter using recursion, as I spent some time trying to understand it… However, it turns out I still don’t understand it. I tried to follow the function line by line, but I don’t get why the function doesn’t stop after num === 0, but it makes num === 2 and executes it until again num === 0, then stops. Can anyone help?

```auto
function convertToRoman(num) {
 var newNum = "";
 var arabNums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  var romanNums = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
  for (var i = 0; i < arabNums.length; i++) {
    if (num >= arabNums[i]) {
      newNum += romanNums[i];
      num -= arabNums[i];
      console.log(num);
      convertToRoman(num);
    }
  }
  return newNum;
}

```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [November 7, 2017, 6:15pm UTC](https://forum.freecodecamp.org/t/roman-numeral-converter-recursion-problem/157823/2 "2017-11-07T18:15:19Z")

</div>

Generally, you want to be using recursion **or** loops, not both - but putting that aside.  
This line `convertToRoman(num);` is not really doing anything. `convertToRoman()` returns a value, but your code does not _do anything_ with that value, so calling the function recursively has no effect.

Another issue is that you are not exiting your loop in the body of your `if` block. Since you are changing the value of `num` in your `if`block, it is possible to to satisfy the condition more than once. This would give you incorrect results.

> **Spoiler: here is a working solution with the minimum of changes to your code**
>
> ```auto
> function convertToRoman(num) {
> var newNum = "";
> var arabNums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
> var romanNums = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
> for (var i = 0; i < arabNums.length; i++) {
> if (num >= arabNums[i]) {
> newNum += romanNums[i];
> num -= arabNums[i];
> return newNum + convertToRoman(num);
> }
> }
> return newNum;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 6:02pm UTC](https://forum.freecodecamp.org/t/roman-numeral-converter-recursion-problem/157823/3 "2021-01-16T18:02:25Z")

</div>

Closed on January 16, 2021, 6:02pm UTC


