# Roman Numerals alg. I am getting zero passes when it works

**URL:** <https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475>\
**Category:** General\
**Created:** [May 5, 2018, 4:03pm UTC](https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475 "2018-05-05T16:03:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jereme2147](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jereme2147/32/101757_2.png) [@Jereme2147](https://forum.freecodecamp.org/u/Jereme2147)\
**Post date:** [May 5, 2018, 4:03pm UTC](https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475/1 "2018-05-05T16:03:01Z")

</div>

There’s a few things not perfect, but “3” for example returns exactly what they’re looking for but it still doesn’t pass the test?  
ps, I realize I used about 10X the amount of code I should have. I’m at a loss. Any ideas? I feel like the tests aren’t working properly, or I’m doing something just slightly off. I know there’s a few issues, but like I said, at least the first 3 tests return exactly what they wanted.

```auto
var result = [];
function convertToRoman(num) {
    var newNum = [];
    pushOnes(num);
    pushFives(num);
    pushTens(num);
    pushHundreds(num);
    pushThousands(num);
    result.reverse();
    num = result.join('');
    return num;
}
//1-5
function pushOnes(num){
   var ones = num%5;
   if (ones != 4){
       for (var i = 0; i < ones; i++){
           result.push('I');
       } 
   } else {
       result.push('IV');
    }
    return num - ones;
}
//5-9
 function pushFives(num){
    var fives = num %10;
    if (fives > 4 && fives < 9){
        result.push('V');
    }else if(fives == 9){
        result.push('IV');
    }
}  
//10-90
function pushTens(num){
    var tens = num % 100;
    if (tens < 40){
        for(var j = 0; j < Math.floor(tens/10); j++){
            result.push('X');
        }
    }else if(tens > 40 && tens <= 50){
        result.push('L');
    }else if(tens >= 60 && tens < 90){
        result.push('L');
        for(var k = 0; k < Math.floor((tens/10)-5); k++){
            result.push('X');
        }
    }else {
        result.push('XC');
    }
}
//100-900
function pushHundreds(num){
    var hundreds = num%1000;
    if (hundreds < 400){
        for(var l = 0; l < (Math.floor(hundreds / 100)); l ++){
            result.push('C');
        }
    }else if(hundreds >= 400 && hundreds <= 500){
            result.push('D');
         }else if(hundreds < 900 && hundreds > 500){
            for(var p = 0; p < ((Math.floor(hundreds / 100)) -5 ); p++){
                result.push('C');
            }
        result.push('D');
        }
    }
//1000+
function pushThousands(num){
    var thousands = Math.floor(num / 1000);
    for(var e = 0; e < thousands; e++){
        result.push('M');
    }
}

convertToRoman(4);
```

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 4:30pm UTC](https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475/2 "2018-05-05T16:30:38Z")

</div>

One rule of these test suites, is never have global variables. Your variable _result_ is getting inherited by each test. It should be inside _convertToRoman()_. Consequently, any function that uses _result_ will also have to be inside _convertToRoman()_ so they can access _result_. When I do that, it starts working for many of the cases.

In general, global variables should be avoided. It’s best if variables have as narrow a scope as is reasonably possible.

---

<div class="post-metadata">

**Author:** ![Jereme2147](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jereme2147/32/101757_2.png) [@Jereme2147](https://forum.freecodecamp.org/u/Jereme2147)\
**Post date:** [May 5, 2018, 5:26pm UTC](https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475/3 "2018-05-05T17:26:33Z")

</div>

Thank you! I’ll make those changes and give it another shot. Thank you so much!

---

<div class="post-metadata">

**Author:** ![Jereme2147](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jereme2147/32/101757_2.png) [@Jereme2147](https://forum.freecodecamp.org/u/Jereme2147)\
**Post date:** [May 5, 2018, 5:32pm UTC](https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475/4 "2018-05-05T17:32:57Z")

</div>

Thank worked. Well it made it pass the tests that it should! I still have some things to fix. Thanks agian!

---

<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 17, 2021, 8:22pm UTC](https://forum.freecodecamp.org/t/roman-numerals-alg-i-am-getting-zero-passes-when-it-works/189475/5 "2021-01-17T20:22:11Z")

</div>

Closed on January 17, 2021, 8:22pm UTC


