Map the Debris: "for ... in" or "for" loops is better?

Hello,
Map the Debris
I have litle pain to well understand when we have to use which loops, particularly between “for” loop and “for in” loop. That why I ask you this question.
For me it’s better to use “For in” with object {…} , and “for” with table. In this exercice the FCC solution 1 used a “for in” loop … , I tested with “for” and that work also. So I don’t know what to think anymore.
Could you clarify this point ?

function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  var a = 2 * Math.PI;
  var newArr = [];

  var getOrbPeriod = function(obj) {
    var c = Math.pow(earthRadius + obj.avgAlt, 3);
    var b = Math.sqrt(c / GM);
    var orbPeriod = Math.round(a * b);
    // create new object
    return {name: obj.name, orbitalPeriod: orbPeriod};
  };

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

  return newArr;
}

// test here
orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }]);

The advantage of for...in is that you don’t need to create an index variable. One less variable, one fewer chance to screw it up, etc.

Also, it is more specific. I worked with a dev that liked to do everything as a while loop - it drove me crazy. If I see a while loop, all I know is that we will be doing something a certain number of times. If I see a for loop, then I know we will be looping a set number of times (but people could mess with it my messing with the for variable). With for…ofandfor…in`, then I know that we will be looping over a data set. I prefer prototype methods because it’s even clearer what is being done. My philosophy is that you should use the most specific method possible.

The more things the code does for you, the better. In part because you can make less mistakes (methods you get from official sources tend to be less flawed). But also because it’s most likely optimized in ways you cannot achieve otherwise. As such, the less things you need to write, the better for performance.

Plus it just looks more professional if you don’t create variables you could avoid.

When you want to go thru the indices on an array (0, 1, 2, 3 and so on), for...in is more suitable, as its more simple and to the point syntax(“loop throught the array indices”). When you want to make more complex logic, like run the loop till something happens, while a condition is true, make computations with the iterator variable, the general for loop gives you those tools. It provides a much wider ability over the loop design. You can iterate the array in reverse, or skip indices etc.
There is also the for...of loop, which is a simple solution for when you dont need the indices, but just iterate throught the array values.
When it comes to objects, for...in gives an easy solution to iterate throught every object property, as you alreayd know.

So for summury:
for … in : specially for iterate every object’s property, And Table if I don’t need an index variable.
for … of : with a Table and I don’t need an index variable.
for : If I need to make something with the index variable.

KEVIN: Do you refer to “private variable” and “public variable” ? The fact that somebody can modify a variable ? Sorry for my english.

while : we will be doing something a certain number of times.
Correct me please if I made a mistake.

Could you add your comments for the ‘for each’ loop ?

Your English is fine.

For example:

for (let i = 0; i <= myArr.length; i++) {
  // ...

Inside that for loop, someone might decide to mess with the i variable. Maybe you will write the code and then someone else comes along and makes a change and messes with that variable, maybe putting an i = 0; or something that screws up the looping, creating an infinite loop. Maybe he adds to it and screws up the loop. Maybe they shadow that variable name. There are a lot of ways to mess it up. With a for...xxx loop, there is no issue because there is no iteration variable. It’s one fewer variable and one fewer chance to mess something up. It also means that as a coder, I don’t have to worry about checking that no one screws it up. There are other ways to screw up, but that one is buttoned up.

Thanks a lot for your explanations :wink:

Maybe its better to show it on display

const arr=['one', 'two', 'three']

for (let index in arr) {
  console.log(index)  //0, 1, 2
  console.log(arr[index]) //one, two, three
}

for (let item of arr) {
  console.log(item)  //one, two, three
}

let lastIndex=arr.length-1  // 2
for (let i=lastIndex; i>=0; i-=2) {
  console.log(i) //2, 0
  console.log(arr[i]) //three, one
}

const obj={
  a: 'alhpa',
  b: 'beta',
  c: 'gamma'
}

for (let key in obj) {
  console.log(key) //a, b, c
  console.log(obj[key])  //alhpa, beta, gamma
}

array
for...in- i loop throught the array indices(can access items via them)
for...of- i loop throught the array items(no access to indices)
for- can create advanced loop logic; in the example code i went throught the array indices in reverse order, while also skipping every other.

object
for...in- simple access to object properties(respectively their values)

1 Like

for...in - i loop throught the array indices(can access items via them)
for...of - i loop throught the array items(no access to indices)
for - can create advanced loop logic; in the example code i went throught the array indices in reverse order, while also skipping every other.

object
for...in - simple access to object properties(respectively their values)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.