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.

根据公式

⭐⭐⭐Map the Debris - 图1

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

  1. Math.pow(5, 2); // 25
  2. Math.pow(7, 4); // 2401
  3. Math.pow(9, 0.5); // 3
  4. Math.pow(-8, 2); // 64
  5. Math.pow(-4, 3); // -64

https://forum.freecodecamp.org/t/javascript-exponents-explained-math-pow-examples/14685

Math.sqrt()

  1. Math.sqrt(9); // 3
  2. Math.sqrt(2); // 1.414213562373095
  3. Math.sqrt(1); // 1
  4. Math.sqrt(0); // 0
  5. Math.sqrt(-1); // NaN
  6. Math.sqrt(-0); // -0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt

  1. function orbitalPeriod(arr) {
  2. var GM = 398600.4418;
  3. var earthRadius = 6367.4447;
  4. var a = 2 * Math.PI
  5. return arr.map(item => {
  6. return {
  7. name: item.name,
  8. orbitalPeriod: Math.round(a * Math.sqrt(Math.pow((earthRadius + item.avgAlt), 3) /GM ))
  9. }
  10. });
  11. }
  12. console.log(
  13. orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }])
  14. )