Skip to content

Instantly share code, notes, and snippets.

@khalid32
Created June 1, 2017 04:42
Show Gist options
  • Save khalid32/e881a71689269c74e53269f3aba5dad7 to your computer and use it in GitHub Desktop.
Save khalid32/e881a71689269c74e53269f3aba5dad7 to your computer and use it in GitHub Desktop.
Freecodecamp's Advanced Algorithm Scripting Challenge
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
arr.forEach(function(obj){
var orbPeriod = Math.round(2*Math.PI*Math.sqrt(Math.pow(earthRadius + obj.avgAlt, 3)/GM));
obj.orbitalPeriod = orbPeriod;
delete obj.avgAlt;
});
return arr;
}
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
return arr.reduce(function(acc, obj){
var orbPeriod = Math.round(2*Math.PI*Math.sqrt(Math.pow(earthRadius + obj.avgAlt, 3)/GM));
acc.push({name: obj.name, orbitalPeriod: orbPeriod});
return acc;
}, []);
}
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment