Roman Numeral Converter issue

Roman Numeral Converter issue
0.0 0

#1

Tell us what’s happening:
I’m trying to solve this challenge (with slightly iffy code) but every time there is a repeated number the code stays stuck in the numeric place, copying that value rather than the intended one. Any help as to why this is happening would be appreciated!

Your code so far

function convertToRoman(num) {
var snum = num.toString();
  var arr1 = snum.split("");
  var arr = [];
  var result = "";
  for (;arr1.length < 4;) {
    arr1.splice(0, 0, '0');
  }
  for (var i = 0; i < arr1.length; i++) {
  if (arr1.indexOf(arr1[i]) === 0){
    if (arr1[i] >= 5 && arr1[i] < 9) {
   arr.push("?");
   for (var w = 5; w < arr1[i]; w++) {
    arr.push("M");  }
 } 
      else if (arr1[i] == 4) { 
   arr.push("M?");
 } 
      else if (arr1[i] == 9) {
   arr.push("M??");
 } 
      else {
    for (var x = 0; x < arr1[i]; x++) {
    arr.push("M");  }
 }
    
  } // End of index search
    
    else if (arr1.indexOf(arr1[i]) === 1) {
    
      if (arr1[i] >= 5 && arr1[i] < 9) {
   arr.push("D");
   for (var f = 5; f < arr1[i]; f++) {
    arr.push("C");  }
 } 
      else if (arr1[i] == 4) { 
   arr.push("CD");
 } 
      else if (arr1[i] == 9) {
   arr.push("CM");
 } 
      else {
    for (var d = 0; d < arr1[i]; d++) {
    arr.push("C");  }
 }
      
  } //End of index search
    
    else if (arr1.indexOf(arr1[i]) === 2) {
      
    if (arr1[i] >= 5 && arr1[i] < 9) {
   arr.push("L");
   for (var n = 5; n < arr1[i]; n++) {
    arr.push("X");  }
 } 
      else if (arr1[i] == 4) { 
   arr.push("XL");
 } 
      else if (arr1[i] == 9 && arr1.indexOf(arr1[i]) === 2) {
   arr.push("XC");
 } 
      else {
    for (var m = 0; m < arr1[i]; m++) {
    arr.push("X");  }
 }
      
  } //End of index search
 
    else if (arr1.indexOf(arr1[i]) === 3) {

      if (arr1[i] >= 5 && arr1[i] < 9) {
   arr.push("V");
   for (var p = 5; p < arr1[i]; p++) {
    arr.push("I");  }
 } 
      else if (arr1[i] == 4) { 
   arr.push("IV");
 } 
      else if (arr1[i] == 9) {
   arr.push("IX");
 } 
      else {
    for (var o = 0; o < arr1[i]; o++) {
    arr.push("I");  }
 }
    
  } //End of index search
    
    result = arr.join("");
  return result;
}
//V = 5
//X = 10
//L = 50
//C = 100
//D = 500
//M = 1000
convertToRoman(3999);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10176.68.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.144 Safari/537.36.

Link to the challenge:


#2

1st this hurts my head (just saying).
2nd I might be wrong but…

// Your main loop starts here
for (var i = 0; i < arr1.length; i++) {

    // Now we have the start of an if statmen. (arr1.indexOf(arr1[i]) === 0) => true
    if (arr1.indexOf(arr1[i]) === 0) {
      // now new if statment. (arr1[i] >= 5 && arr1[i] < 9) => false
      if (arr1[i] >= 5 && arr1[i] < 9) {
        arr.push("?");        
        for (var w = 5; w < arr1[i]; w++) {
          arr.push("M");
        } 
      // (arr1[i] == 4) => false
      } else if (arr1[i] == 4) {
        arr.push("M?");
      // (arr1[i] == 9) => false
      } else if (arr1[i] == 9) {
        arr.push("M??");
      } else {
        // so we hit the else and run this code
        for (var x = 0; x < arr1[i]; x++) {
          arr.push("M");          
        }
      }
      // now the main if statment is over
    } // End of index search

#3

Let me walk you through what your code does with the number 244.

Before the outer for loop starts, you have arr = [] and arr1 = [‘0’, ‘2’, ‘4’, ‘4’]

Inside the outer for loop:

1st iteration: i = 0, so arr1[0] = ‘0’ and arr1.indexOf(arr1[i]) === 0) evaluates to true because arr1.indexOf(arr1[0]) returns 0. Nothing gets pushed to arr, because the else block of code of the first if statement executes, but since arr1[0] is ‘0’, the for loop does not do anything.

2nd iteration: i = 1, so arr1[1] = ‘2’ and arr1.indexOf(arr1[i]) === 0) evaluates to false so we check the 1st else if statement which returns true because arr1.indexOf(arr1[1]) returns 1. The else block of code executes, because the nested if/else if statements evaluate to false. The for loop iterates twice pushing ‘C’ each time to arr. At this point arr = [‘C’, ‘C’]

3rd iteration, i = 2, so arr1[2] = 4. This time the 2nd else if (seen below) evaluates to true, because arr1.indexOf(arr1[2]) returns 2

if (arr1.indexOf(arr1[i]) === 2) {

The nested if else block which pushes ‘XL’ executes, because arr1[2] = 4. At this point arr = [‘C’, ‘C’, 'XL].

4th iteration: i = 3, so arr[3] = 4. This time the 2nd else if (same as last iteration) evaluates to true. Why? Because arr1.indexOf(arr1[3]) returns 2? What??? Remember, arr1 is [ ‘0’, ‘2’, ‘4’, ‘4’ ], so since arr1[3] is ‘4’ and the first index containing ‘4’ in arr1 is index 2 (the 3rd element), this causes the 2nd nested else if’s code block to run. And just like the 3rd iteration, since arr1[3] is 4, the following else if causing ‘XL’ to get pushed into arr again.

} else if (arr1[i] == 4) {

Since this is the last iteration of the for loop, you return the joined arr array as ‘CCXLXL’.


#4

Weird, thanks for the help! I just went to the answer to figure out what I had done wrong and realised that the entire thing just created a loop error, so that’s that I guess.