Map the Debris - why is this happening?

I’m having difficulty understanding why my code does not work.
I am using a forEach loop to push elements into retArr. However, retArr results in 3 copies of ‘moon’ instead.
Any explanation of why this is happening would be greatly appreciated!

Your code so far

function orbitalPeriod(arr) {
  var retArr = [];
  var retObj = {};
  
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  
  arr.forEach((currElem,index)=>{
    
    var a = currElem.avgAlt + earthRadius;
  
    retObj.name = currElem.name;
    retObj.orbitalPeriod = Math.round(2*Math.PI*Math.sqrt(Math.pow(a,3)/GM),0);
    
    retArr.push(retObj);
    
  });
  
  return retArr;
}
console.log('start');
orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]);
console.log('end');```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/map-the-debris

This happens because you are pushing the same object 3 times to array. Make a new one every iteration. You can add: retObj ={}; at the beginning of the loop.

Thank you, Furon. Adding that one line does indeed work…but why?
It seems that if my original code was pushing the same object 3 times, then why does adding retObj ={} to the beginning of my loop make it work? Does retObj ={} make retObj a new object even though it was declared outside the loop?

Exactly. It makes a new object. retObj is only a variable which stores a reference to an object and you change this reference to another (new) object.

Gotcha. Makes sense now, thank you for your help!