My Roman Numeral code give right Answer but cant complete the challenge

My Roman Numeral code give right Answer but cant complete the challenge
0

#1

Here my code, why the challenge wont complete?

var romanArr =["I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"].reverse();
var numeralArr=[1,4,5,9,10,40,50,90,100,400,500,900,1000].reverse();
var resultArr=[];

function convertToRoman(num) {
  var i =num;
   while(i > 0){

       for(b=0;b<romanArr.length; b++){

           if(num >= numeralArr[b]){
                num -= numeralArr[b];
                resultArr.push(romanArr[b]);
             break;
           }
         }
         i--;
     } 

return resultArr.join("");


}

convertToRoman(36);

#2

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

#3

The FCC tests run each test consecutively, so anything declared globally will be global to all the tests. Because you declared your resultArr globally out side of convertToRoman, resultArr retains it’s last value from each previous test run. Move it inside your function so that it gets reset back to a blank array during each test.


#4

Oh, I forgot about that :sweat_smile:! Thank’s for this clean explanation!