Skip to content

Commit e3ce72c

Browse files
committed
Complete map the debris algorithm
1 parent 1a64517 commit e3ce72c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
2323
- [Everything Be True](#everything-be-true)
2424
- [Arguments Optional](#arguments-optional)
2525
- [Make a Person](#make-a-person)
26+
- [Map the Debris](#map-the-debris)
2627

2728
#### Sum All Numbers In a Range
2829

@@ -457,3 +458,28 @@ export function Person(firstAndLast) {
457458
this.setLastName = (last) => fullName = fullName.split(' ')[0] + ' ' + last;
458459
}
459460
```
461+
462+
#### Map the Debris
463+
464+
Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).
465+
466+
The array will contain objects in the format `{name: 'name', avgAlt: avgAlt}`.
467+
468+
You can read about orbital periods on Wikipedia.
469+
470+
The values should be rounded to the nearest whole number. The body being orbited is Earth.
471+
472+
The radius of the earth is 6367.4447 kilometers, and the GM value of earth is 398600.4418 km3s-2.
473+
474+
```javascript
475+
export function orbitalPeriod(arr) {
476+
const GM = 398600.4418;
477+
const earthRadius = 6367.4447;
478+
479+
return arr.map((object) => {
480+
const a = Math.pow(earthRadius + object.avgAlt, 3);
481+
const computedOrbitalPeriod = Math.round(Math.PI * 2 * Math.sqrt(a / GM));
482+
return { name: object.name, orbitalPeriod: computedOrbitalPeriod };
483+
});
484+
}
485+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function orbitalPeriod(arr) {
2+
const GM = 398600.4418;
3+
const earthRadius = 6367.4447;
4+
5+
return arr.map((object) => {
6+
const a = Math.pow(earthRadius + object.avgAlt, 3);
7+
const computedOrbitalPeriod = Math.round(Math.PI * 2 * Math.sqrt(a / GM));
8+
return {name: object.name, orbitalPeriod: computedOrbitalPeriod };
9+
});
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { orbitalPeriod } from "../../src/fcc-intermediate-algorithms/map_the_debris";
2+
3+
test('should calculate orbital period', () => {
4+
expect(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])).toEqual([{name: "sputnik", orbitalPeriod: 86400}]);
5+
});

0 commit comments

Comments
 (0)