function orbitalPeriod(arr) {
const GM = 398600.4418;
const earthRadius = 6367.4447;
const newArr = [];
//Looping through each key in arr object
for (let elem in arr) {
//Rounding off the orbital period value
const orbitalPer = Math.round(
2 * Math.PI * Math.sqrt(Math.pow(arr[elem].avgAlt + earthRadius, 3) / GM)
);
//Adding new object with orbitalPeriod property
newArr.push({name: arr[elem].name, orbitalPeriod: orbitalPer});
}
return newArr;
}
// test here
orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }]);
This is from a freeCodeCamp challenge (Link)
Why is 'orbitalPer' declared as a const inside the for loop? Once a const is declared with value, it is not possible to change the value isn't it? Here if the array has more than one object, when the loop runs more than once, wont having const encounter an error?