SOLVED: outer and inner loop were using same variable (i). Original Question: JavaScript for-loop loops only once when comparing i to an array element

Solved by multiple answers below. Thanks for the answers. The problem was that my for-loop was inside an outer while-loop, and both used the variable ‘i’ for their loops.

Full code at: https://jsbin.com/sikeqizoda/1/edit?js,console

and the problem loop has a /**************…..*/ before and after it.

I’m doing one of the projects on freeCodeCamp and I’m encountering an issue where I have this for-loop:

for (let i = 0; i < arr[i]; i++) {
    newArr.push("M");
}

in which I’m comparing an array element, in this case, arr[i] = 3, with typeof being number, and where i also has typeof = number. Again, in this situation, arr[i] = 3, and if I run the code as above, it’s supposed to loop 3 times, but only loops once (only one "M" is pushed into my newArr array).

But, if I put the number 3 in place of arr[i]:

for (let i = 0; i < 3; i++) {
    newArr.push("M");
}

it loops 3 times as I want it to.

I have tried putting a console.log(arr[i]) underneath there and it logs 3.

console.log(i) logs 0.

and newArr becomes ['M'].

I’ve also tried changing arr[i] to parseInt(arr[i]), and console.log(parseInt(arr[i]) == 3) returns true! It’s so confusing, why does ‘3’ work, but arr[i] which is equivalent doesn’t?

I don’t understand what the issue is. Could someone please help?

Thanks for your time.

>Solution :

The issue is because you’ve got your outer while loop iterating over i, then you declare multiple inner loops, also iterating over i, and the two are conflicting. Always use different variables for nested loops:

let one = { 0: "I", 1: "X", 2: "C", 3: "M" }
let four = { 0: "IV", 1: "XL", 2: "CD" }
let five = { 0: "V", 1: "L", 2: "D" }
let nine = { 0: "IX", 1: "XC", 2: "CM" }

function consecutiveZerosAfter(index, arr) {
  let numZeros = 0;
  for (let i = index + 1; i < arr.length; i++) {
    if (arr[i] == 0) {
      numZeros += 1;
    } else {
      return numZeros
    }
  }
  return numZeros
}

function arrayToRoman(arr) {
  let newArr = [];
  let counter = 0;

  let i = 0;
  while (i < arr.length) {
    if (arr[i] == 0) {
      i++;
    } else {
      if (arr[i] >= 1 && arr[i] <= 3) {
        switch (consecutiveZerosAfter(i, arr)) {
          case 0:
            for (let n = 0; n < arr[i]; n++) {
              newArr.push("I");
            }
            break;
          case 1:
            for (let n = 0; n < arr[i]; n++) {
              newArr.push("X");
            }
            break;
          case 2:
            for (let n = 0; n < arr[i]; n++) {
              newArr.push("C");
            }
            break;
            /****************************************************************************************/
          case 3:
            for (let n = 0; n < arr[i]; n++) {
              newArr.push("M");
            }
            break;
            /***************************************************************************************/
        }
      } else if (arr[i] == 4) {

      } else if (arr[i] >= 5 && arr[i] <= 8) {

      } else if (arr[i] == 9) {

      }
    }
    i++;
  }
  return newArr
}

console.log(arrayToRoman([3, 0, 0, 0]));

Leave a Reply