Skip to content

Instantly share code, notes, and snippets.

@elisavetTriant
Created July 17, 2019 09:56
Show Gist options
  • Save elisavetTriant/8bacf1e2393baf5cab9c64bf6f559a0f to your computer and use it in GitHub Desktop.
Save elisavetTriant/8bacf1e2393baf5cab9c64bf6f559a0f to your computer and use it in GitHub Desktop.
/*Intermediate Algorithm Scripting: Map the Debris
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.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris*/
function orbitalPeriod(arr) {
const GM = 398600.4418; //in km3s-2.
const earthRadius = 6367.4447; //in km
let resultArray = [];
function findOrbitalPeriod(avgAlt){
//Using Kepler's Third Law, the orbital period T (in seconds), rounded, when given the AvgAlt of the body orbiting Earth.
//See more on https://en.wikipedia.org/wiki/Orbital_period
return Math.round(2 * Math.PI * Math.sqrt(Math.pow(earthRadius + avgAlt, 3) / GM));
}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
resultArray = arr.map(obj =>{
let rObj = {};
rObj.name = obj.name;
rObj.orbitalPeriod = findOrbitalPeriod(obj.avgAlt)
return rObj;
});
return resultArray;
}
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
/*
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]) should return [{name: "sputnik", orbitalPeriod: 86400}].
Passed
orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]) should return [{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}].
Passed
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment