Works on repl.it doesn't work no freeCodeCamp - JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter

Works on repl.it doesn't work no freeCodeCamp - JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter
0

Hi freeCodeCampers, just finished my Roman Numeral Converter, and it works fine on repl.it , but on the freecodecamp the output is:

// running tests
convertToRoman(2) should return "II".
convertToRoman(3) should return "III".
convertToRoman(4) should return "IV".
convertToRoman(5) should return "V".
convertToRoman(9) should return "IX".
convertToRoman(12) should return "XII".
convertToRoman(16) should return "XVI".
convertToRoman(29) should return "XXIX".
convertToRoman(44) should return "XLIV".
convertToRoman(45) should return "XLV"
convertToRoman(68) should return "LXVIII"
convertToRoman(83) should return "LXXXIII"
convertToRoman(97) should return "XCVII"
convertToRoman(99) should return "XCIX"
convertToRoman(400) should return "CD"
convertToRoman(500) should return "D"
convertToRoman(501) should return "DI"
convertToRoman(649) should return "DCXLIX"
convertToRoman(798) should return "DCCXCVIII"
convertToRoman(891) should return "DCCCXCI"
convertToRoman(1000) should return "M"
convertToRoman(1004) should return "MIV"
convertToRoman(1006) should return "MVI"
convertToRoman(1023) should return "MXXIII"
convertToRoman(2014) should return "MMXIV"
convertToRoman(3999) should return "MMMCMXCIX"
// tests completed

check my code please:

let arr = [];
function splitNumber(num) {
  let len = num.toString().length; //get length of the number
  let d = Math.pow(10, len);


  for (let i = 0; i < len + 1; i++) {
    const x = num % d; // remainder from num to d save number by unit, decimal, etc..
    if (d > 1) { 
        d /= 10;
        arr.push(Math.floor(x / d) * d); // save the value in the array
    }
  }
  return arr;
}

function convertToRoman(num) {
  splitNumber(num);
  let arabicToRoman= {
   1: 'I',
   4: 'IV',
   5: 'V',
   9: 'IX',
   10: 'X',
   40: 'XL',
   50: 'L',
   90: 'XC',
   100: 'C',
   400: 'CD',
   500: 'D',
   900: 'CM',
   1000: 'M'
 };
 let array = [];
  for (let i = 0; i < arr.length; i++) {
      let a = arr[i]
    if (a > 1 && a < 4) {
      array.push(arabicToRoman[1].repeat(a));
    } else if (a > 5 && a < 9) {
      array.push(arabicToRoman[5],arabicToRoman[1].repeat(a - 5));
    } else if (a > 10 && a < 40) {
      array.push(arabicToRoman[10].repeat(a / 10));
    } else if (a > 50 && a < 90) {
      array.push(arabicToRoman[50],arabicToRoman[10].repeat(a / 10 - 5));
    } else if (a > 100 && a < 400) {
      array.push(arabicToRoman[100].repeat(a / 100));
    } else if (a > 500 && a < 900) {
      array.push(arabicToRoman[500],arabicToRoman[100].repeat(a / 100 - 5));
    } else if (a > 1000) {
      array.push(arabicToRoman[1000],arabicToRoman[1000].repeat(a / 1000));
    } else {
      array.push(arabicToRoman[a]);
    };
  };
  num = array.toString().replace(/,/g,'')
  return(num);
};

convertToRoman(36);

any suggestions ?
thanks

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 previous 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

Also, you can enable strict mode in your repl, as that is also enabled in freecodecamp editor
to do this, you can write "use strict" (quote included) as first line of your repl

thanks, it worked :slight_smile:

1 Like