Please consider the code below:
The Code
function orbitalPeriod(arr) {
const GM = 398600.4418;
const earthRadius = 6367.4447;
return arr.map(({ name, avgAlt }) => {
const earth = earthRadius + avgAlt;
/*here*/orbitalPeriod = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earth, 3)/GM));
return { name, orbitalPeriod };
});
}
Why the function returns orbitalPeriod
even though it was not declared? can someone explain please?!
It was declared. Your function is called orbitalPeriod
. Then within the function, you reassign it to a number, so you can’t use the function a second time:
function orbitalPeriod(arr) {
const GM = 398600.4418;
const earthRadius = 6367.4447;
return arr.map(({ name, avgAlt }) => {
const earth = earthRadius + avgAlt;
console.log('within 1', orbitalPeriod) // function
/*here*/orbitalPeriod = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earth, 3)/GM));
console.log('within 1', orbitalPeriod) // number
return { name, orbitalPeriod };
});
}
console.log('outside 1', orbitalPeriod) // function
/* first function call */
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
console.log('outside 2', orbitalPeriod) // number
/* second function call */
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
// Uncaught TypeError: orbitalPeriod is not a function
1 Like
Thank you very much for explaining