Delete operator/Math functions not working how i expect(Map the Debris)

Tell us what’s happening:

Your code so far


function orbitalPeriod(arr) {
  let GM = 398600.4418;
  let earthRadius = 6367.4447;
  let orbit = 2 * Math.PI * Math.sqrt(Math.pow(earthRadius + arr.avgAlt, 3)/GM)
  delete arr.avgAlt
  arr.orbitalPeriod = Math.round(orbit)
  return arr;

}

console.log(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris

When i log the test to the console in the code above, my orbit variable is NaN. I can’t understand why that would be i’m only working with numbers here. Also, the delete operator is not deleting the avgAlt key. Before i go looking at the solution I’d like to know why the above is not working, any input would be appreciated.

arr is not the object it is the array, arr[0] would be the (first) object.

1 Like

In the orbitalPeriod function you are treating arr as if it is an object, when in fact it is an array. Functions like delete don’t work on arrays, and also arrays cannot have user-defined properties like avgAlt or orbitalPeriod. Objects can however. You can access the objects contained in arr via the arr[i] syntax, where i is an integer.

1 Like