Help me please with this code

Help me please with this code
0.0 0

#1

hi there!

i wrote down the roman numbers code for many times, i can’t make it work :-/
i tried “.indexOf()” etc, but no luck. and then i decided to use alternative way.
according to code, there is no mistake/error, but it doesn’t work properly.

when i use a number which is less than 4 level, it shows still the level from thousand level…

could you check it please, and help me?
thank you in advance :slight_smile:

`function arr(num) {
var result = [];
result[0] = sayi.toString();
thousand = thousandLevel(bos[0][0]);
hundred = hundredLevel(bos[0][1]);
tens = tensLevel(bos[0][2]);
ones = onesLevel(bos[0][3]);

function onesLevel(x) {
if ((x === “0”)) {
console.log("");
//return “”;
} else if (x === “1”) {
console.log(“I”);
} else if (x === “2”) {
console.log(“II”);
} else if (x === “3”) {
console.log(“III”);
} else if (x === “4”) {
console.log(“IV”);
} else if (x === “5”) {
console.log(“V”);
} else if (x === “6”) {
console.log(“VI”);
} else if (x === “7”) {
console.log(“VII”);
} else if (x === “8”) {
console.log(“VIII”);
} else if (x === “9”) {
console.log(“IX”);
}
}

function tensLevel(x) {
if ((x === “0”) || (x === “”)) {
console.log("");
} else if (x == “1”) {
console.log(“X”);
} else if (x == “2”) {
console.log(“XX”);
} else if (x == “3”) {
console.log(“XXX”);
} else if (x == “4”) {
console.log(“XXXL”);
} else if (x == “5”) {
console.log(“L”);
} else if (x == “6”) {
console.log(“LX”);
} else if (x == “7”) {
console.log(“LXX”);
} else if (x == “8”) {
console.log(“LXXX”);
} else if (x == “9”) {
console.log(“XC”);
}
}

function hundredLevel(x) {
if ((x === “0”) || (x === “”)) {
console.log("");
} else if (x === “1”) {
console.log(“C”);
} else if (x === “2”) {
console.log(“CC”);
} else if (x === “3”) {
console.log(“CCC”);
} else if (x === “4”) {
console.log(“CD”);
} else if (x === “5”) {
console.log(“D”);
} else if (x === “6”) {
console.log(“DC”);
} else if (x === “7”) {
console.log(“DCC”);
} else if (x === “8”) {
console.log(“DCCC”);
} else if (x === “9”) {
console.log(“CM”);
}
}

function thousandLevel(x) {
if ((x == 0) || (x == "")) {
  console.log("a");
} else if (x === "1") {
  console.log("M");
} else if (x === "2") {
  console.log("MM");
} else if (x === "3") {
  console.log("MMM");
} else if (x === "4") {
  console.log("MMMM");
} else if (x === "5") {
  console.log("MMMMM");
} else if (x === "6") {
  console.log("MMMMMM");
} else if (x === "7") {
  console.log("MMMMMMM");
} else if (x === "8") {
  console.log("MMMMMMMM");
} else if (x === "9"){
  console.log("MMMMMMMMM");
}

}

function sum(result) {
  if (result[0].length === 4) {
    console.log(thousand + hundred +  tens + ones);
  } else if (result[0].length === 3) {
    console.log(hundred +  tens + ones);
  } else if (result[0].length === 2) {
    console.log(tens + ones);
  } else if (result[0].length === 1) {
    console.log(ones);
  }

}

sum(result);

}

arr(525);`


#2

why not just use arrays of roman numbers and decimal numbers.


#3

i tried, but i couldn’t make it work.
But i’ll try again. thank you for your suggestion.


#4

this will be a start for you

function convertToRoman(num) {
  var decimalValues = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
  var romanNumerals = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];
  if(num < 0){
    return "Change me";
  }

#5

:+1:
thank you, i’ll give a shot again


#6

ok, i tried some code like yours, and it worked. but challenge window doesn’t approve :-/
i think, i have to use .join(), .indexOf and .splice()… i tried splice, but it didn’t work.
could you help me please, what i have to do next?

function convertToRoman(num) {
  var result = "";
  result = num.toString().split(",");
  result = result.join();
  var x = result.length;
  var roman = "";
  var binler = ["", "M", "MM", "MMM"];
  var yuzler = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
  var onlar = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
  var birler = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
  function sum(arr) {
    if (x === 4) {
      console.log(binler[arr[0]] + yuzler[arr[1]] + onlar[arr[2]] + birler[arr[3]]);
    } else if (x === 3) {
      console.log(binler[0] + yuzler[arr[0]] + onlar[arr[1]] + birler[arr[2]]);
    } else if (x === 2) {
      console.log(binler[0] + yuzler[0] + onlar[arr[0]] + birler[arr[1]]);
    } else if (x === 1) {
      console.log(binler[0] + yuzler[0] + onlar[0] + birler[arr[0]]);
    } return result;
  } 
 return sum(result);
}

convertToRoman(1399);

#7

and this is the version with .splice(), but still doesn’t accept:

function convertToRoman(num) {
  var result = "";
  result = num.toString().split("");
  
  //result = result.join();
  var x = result.length;
  var roman = "";
  var binler = ["", "M", "MM", "MMM"];
  var yuzler = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
  var onlar = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
  var birler = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];

  function sum(arr) {
  if (x === 4) {
     var y = arr.splice(0, 1,binler[arr[0]]);
    console.log(binler[y] + yuzler[arr[1]] + onlar[arr[2]] + birler[arr[3]]);
   } else if (x === 3) {
     var z = arr.splice(0, 1,yuzler[arr[0]]);
     console.log(yuzler[z] + onlar[arr[1]] + birler[arr[2]]);
    }  else if (x === 2) {
     var k = arr.splice(0, 1,onlar[arr[0]]);
     console.log(onlar[k] + birler[arr[1]]);
    } else if (x === 1) {
     var i = arr.splice(0, 1, birler[arr[0]]);
     console.log(birler[i]);
    }
  } return sum(result);
}

convertToRoman(3999);

#8

you are not calling the the function sum(arr)


#9

i tried to call, but i couldn’t get it work…