JavaScript Orbital Period
Requirements
- The orbital period is the time a give astronomical object takes to complete one orbit around another object.
- For a small body orbiting a central body in a circular or elliptic orbit, the orbital period T (in seconds) is:
T = 2 * PI * sqrt(a^3 / GM)
- where
a
is the orbit's semi-major axis,GM
is the product ofG
(gravitational constant) andM
(mass of the larger body). - In this application the body being orbited is the Earth.
- Ref: https://en.wikipedia.org/wiki/Orbital_period
Implementation
orbitalPeriod(arr)
calculates the orbital period of ojects around the Earth in seconds. Input array arr
is an array of objects in the format: { name: 'name', avgAlt: avgAlt }
where avgAlt
is the object's average altitude in kilometers. Returns an array of objects in the format: { name: 'name', orbitalPeriod: T }
where T
is the orbital period in seconds.
Similarly orbitalPeriodHours(arr)
calculates the orbital period around the Earth in hours, and orbitalPeriodDays(arr)
calculates the orbital period in days.
Example Usage
Use the browser console to interact with the function (Ctrl + Shift + I
, then select the "Console" tab):
> orbitalPeriod([{ name : "sputnik", avgAlt : 35873.5553 }]);
// Returns: [{ name: 'sputnik', orbitalPeriod: 86400 }]
> orbitalPeriodHours([{ name : "sputnik", avgAlt : 35873.5553 }]);
// Returns: [{ name: 'sputnik', orbitalPeriodHours: 24 }]
> orbitalPeriodDays([{ name: "moon", avgAlt: 378632.553 }]);
// Returns: [{ name: 'moon', orbitalPeriodDays: 28 }]