Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Return a new array that transforms the element's average altitude into their orbital periods. The array will contain objects in the format {name: 'name', avgAlt: avgAlt}. You can read about orbital periods on wikipedia. The values should be rounded to the nearest whole number. The body being orbited is Earth. The radius of the earth is 6367.4447…
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
//create a function to calculate orbital period
function calcPeriod(avgAlt){
var orbitalRadius = earthRadius + avgAlt;
//calculation is 2 multiplied by pi multiplied by the square root of the
//orbital radius to the power of 3 divided by the GM
var period = Math.round(2*Math.PI*(Math.sqrt(Math.pow(orbitalRadius,3)/GM)));
return period;
}
//for every object in the array, work out the Orbital Period, add it to the
//object and remove the avgAlt entry.
arr.forEach(function(el){
el.orbitalPeriod = calcPeriod(el.avgAlt);
delete el.avgAlt;
});
return arr;
}
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.