Roman Numeral Converter issue

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:
https://www.freecodecamp.org/challenges/roman-numeral-converter

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

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.