Question on a for loop

Question on a for loop
0.0 0

#1

Hello! I had a question on a for loop for a solution for the steam roller challenge

The code is:

function steamrollArray(arr) {
var newArray = [];
var flatten = function(arg) {
  if(!Array.isArray(arg)){
    newArray.push(arg);
  } else {
    for(var a in arg) {
      flatten(arg[a]);
    }  
  }
};
  arr.forEach(flatten);
  return newArray;
}

My question is how come in the for loop you have to use for(var a in arg) and instead cannot use something like for(var i = 0; i<arg.length; i++)? I understand everything else about the code.


#2

hey @njanne19! Have you checked out the documentation for for...in on MDN?


#3

It is possible to use a for loop in your code. I did use a for loop in my code. However, I did nest it in a while loop.

Here is my code:

function steamrollArray(arr) {
  // I'm a steamroller, baby
  
  var isFlattened = false;
  
  while (isFlattened === false) {
    for (var i = 0; i < arr.length; i++) {
      if (Array.isArray(arr[i])) {
          arr = arr.reduce(function(a, b) { // Reduces the array by one dimension.
          return a.concat(b);
        }, []);
        i--; // Needed in order to check if element at this index is still an array.
      }
    }
    isFlattened = true;
  }
  
  return arr;
}

#4

@njanne19 - You should be able to use your version of the for loop also. Give it a try. Replace the for/in loop in your code with a standard for loop:

for (var i = 0; i<arg.length; i++) {
flatten(arg[i]);
}

The for/in loop does the same thing as above. I think it is used because it takes less keystrokes to write. There are a few other loop types as well, there is also ‘while’ loops and ‘do/while’ loops that could be used to. Honestly, I think that you could use whatever you are most comfortable with. I tend to use the good old standard ‘for’ loop.


#5

There’s also a pretty sweet way to solve this challenge with just recursion and spread operator in place of recursion + loops.

The for...in loop is more commonly used on objects, it is used to iterate over an object’s properties, but in an arbitrary order - so i think a regular for loop or a for...of loop would be safer in the original example.

    function forIn(arr) {
      for(var idx in arr) { 
         console.log(idx, arr[idx]); // <--- (indexes, items)
      }
    }  

    forIn(['a', 'b', 'c', 'd']);

    // logs: 0, 1, 2, 3 <--- indexes
    // logs: a, b, c, d <--- items

for...of loops are more applicable to iterating over an array’s elements:

    function forOf(arr) {
      for(var el of arr) { 
         console.log(el); // <--- (items)
      }
    }  

    forOf(['a', 'b', 'c', 'd']);

    // logs: a, b, c, d

so this would be just as effective and potentially more reliable (at leas according to documentation):

    for(var a of arg) {
        flatten(a);  // does the same thing
    }  

BUT, if anyone is interested, the spread and recursion syntax is the shortest and most fun! Worth looking up, it’s out there.


#6

Thank you for the help. All of your tips have worked well