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.
根据公式
Math.PI
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/PI
Math.round() 四舍五入取整
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
Math Pow
Math.pow()
returns the value of a number to the power of another number.
Syntax
Math.pow(base, exponent)
, where base
is the base number and exponent
is the number by which to raise the base
.
pow()
is a static method of Math
, therefore it is always called as Math.pow()
rather than as a method on another object.
Examples
Math.pow(5, 2); // 25
Math.pow(7, 4); // 2401
Math.pow(9, 0.5); // 3
Math.pow(-8, 2); // 64
Math.pow(-4, 3); // -64
https://forum.freecodecamp.org/t/javascript-exponents-explained-math-pow-examples/14685
Math.sqrt()
Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095
Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
var a = 2 * Math.PI
return arr.map(item => {
return {
name: item.name,
orbitalPeriod: Math.round(a * Math.sqrt(Math.pow((earthRadius + item.avgAlt), 3) /GM ))
}
});
}
console.log(
orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }])
)