Created
June 16, 2018 02:11
-
-
Save eday69/726025b97d3f0fccf6c3ffd2f6000751 to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediate Algorithm Scripting: Map the Debris
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Return a new array that transforms the elements' average | |
// altitude into their orbital periods (in seconds). | |
// 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 kilometers, and the GM | |
// value of earth is 398600.4418 km3s-2. | |
function orbitalPeriod(arr) { | |
var GM = 398600.4418; | |
var earthRadius = 6367.4447; | |
return arr.map(satellite => { | |
let alt=satellite.avgAlt; | |
delete satellite.avgAlt; | |
satellite["orbitalPeriod"] = Math.round(2 * Math.PI * Math.sqrt( Math.pow((alt+earthRadius),3) / GM)); | |
return satellite; | |
}); | |
} | |
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]); // [{name: "sputnik", orbitalPeriod: 86400}] | |
orbitalPeriod([{name: "iss", avgAlt: 413.6}, | |
{name: "hubble", avgAlt: 556.7}, | |
{name: "moon", avgAlt: 378632.553}]); // [{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}]. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment