Roman Number help to post my sollution

Roman Number help to post my sollution
0

#1

Hi,
The result return the right answer, but FCC still don’t accept it:

  var castStringParam = void 0,
      obj = {},
      add = '',
      result = [],
      arrRomanOne = [{ key: 1, valeur: 'I' }, { key: 2, valeur: 'II' }, { key: 3, valeur: 'III' }, { key: 4, valeur: 'IV' }, { key: 5, valeur: 'V' }, { key: 6, valeur: 'VI' }, { key: 7, valeur: 'VII' }, { key: 8, valeur: 'VIII' }, { key: 9, valeur: 'IX' }],
      arrRomanTwo = [{ key: 1, valeur: 'X' }, { key: 2, valeur: 'XX' }, { key: 3, valeur: 'XXX' }, { key: 4, valeur: 'XL' }, { key: 5, valeur: 'L' }, { key: 6, valeur: 'LX' }, { key: 7, valeur: 'LXX' }, { key: 8, valeur: 'LXXX' }, { key: 9, valeur: 'XC' }],
      arrRomanThree = [{ key: 1, valeur: 'C' }, { key: 2, valeur: 'CC' }, { key: 3, valeur: 'CCC' }, { key: 4, valeur: 'CD' }, { key: 5, valeur: 'D' }, { key: 6, valeur: 'DD' }, { key: 7, valeur: 'DDD' }, { key: 8, valeur: 'DCCC' }, { key: 9, valeur: 'CM' }],
      arrRomanFour = [{ key: 1, valeur: 'M' }, { key: 2, valeur: 'MM' }, { key: 3, valeur: 'MMM' }, { key: 4, valeur: 'M' }, { key: 5, valeur: 'M' }, { key: 6, valeur: 'M' }, { key: 7, valeur: 'M' }, { key: 8, valeur: 'M' }, { key: 9, valeur: 'M' }];
  //num, arrRoman[j].key, arrRoman[j].valeur
  function oneNumber(num, obj, whichArray) {
    //loop in the array
    for (var j = 0; j < obj.length; j++) {
      //console.log(obj[j].key +' : '+ obj[j].valeur+' : '+j+' : '+num);
      if (obj[j].key == num) {
        add = obj[j].valeur;
      }
    }
    return result.push(add);
  }
  function convertToRoman(num) {
    castStringParam = num.toString();
    castStringParam.split('').map(function (a, i) {

      //console.log(a, i);
      //for (var k = 0; k <= i; k++) {
      //console.log(`${a} ${arrFun[i]}`);
      //console.log(i);
      //if (i == arrFun[i]) {
      ////console.log(arrFun[i]);
      //oneNumber(a, arrFun[i] )
      //}
      //}

      if (i === 0) {
        oneNumber(a, arrRomanOne);
      } else if (i === 1) {
        oneNumber(a, arrRomanTwo);
      } else if (i === 2) {
        oneNumber(a, arrRomanThree);
      } else if (i === 3) {
        oneNumber(a, arrRomanFour);
      }
    });
    //console.log(result = result.reverse().join('').toString());
    result = result.reverse().join('').toString();
    //console.log(typeof result);
    //console.log(result);
    return result;
  }
  convertToRoman(3);

#2

It won’t be accepted for some reason if you have global variables.


#3

WoooooOOOwwww
For all challenge ?
That’s limit a lot, I want to use ES6 and so the let variable don’t work too :frowning:


#4

You can move the variables and the other function inside the scope of the convertToRoman function.


#5

Thanks I did, I will use an IIFE and I will try the recursivity.
that give me 3 times more code, :frowning:


#6

It won’t be accepted for some reason if you have global variables.

Can you explain to me the reasons

Thank you


#7

Sorry I can’t answer that. That’s why I added “for some reason”

It’s an open issue though. You can read more here.


#8

I tought your was thinking about the coding technics.
To use a global variable is not the best practice.