Solving "Map the Debris" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-08-07 13:22:27

---
title: Solving "Map the Debris" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's intermediate algorithm challenge, "Map the Debris" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-037-map-the-debris
series: Solving freeCodeCamp's Algorithm Challenges
---

** *Post can also be found on [virenb.cc](https://virenb.cc/fcc-037-map-the-debris)* **

!['Map the Debris' Challenge](//images.ctfassets.net/yzwpq1epaq68/727ns4fu4ERXtvEPpuGHRm/1b4688af6678bb6bfb91cb99b4194d18/ss-037.png)

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Map the Debris'.



### Starter Code

```javascript
function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  return arr;
}

orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
```



### Instructions

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](http://en.wikipedia.org/wiki/Orbital_period).

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.



### Test Cases

- `orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])` should return `[{name: "sputnik", orbitalPeriod: 86400}]`.
- `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}]`.



# Our Approach

The instructions for this challenge are short and to the point. 

- Our input is an array of one or more objects.

- We have to return the same array of objects, but altering one of the key-value pairs (`avgAlt` -> `orbitalPeriod`).

- Make sure orbitalPeriod is calculated in seconds and rounded up to the nearest whole number.

  

The last challenge seems a bit hard. This formula is a little complex. But basically we have to do some math to calculate it, deleted `avgAlt`, and add in `orbitalPeriod` into each object in the array.



After a lot of Internet searching (formerly known as Googling - I don't like to feed into the search engine bias), I came across the correct formula, `T = 2 * pi * sqrt(r^3/GM); r = earthRadius + avgAlt`.



GM is provided in the instructions, as is the radius of the Earth. We can start by setting those variables.



```javascript
const GM = 398600.4418;
const earthRadius = 6367.4447;
```



Now that we have our values and a formula, we can plug in those values and do some math. Once we have the orbital period value, we can add it to each object, and remove the `avgAlt`. We can loop over each object in the array using `map()`.



```javascript
arr.map(obj => {
  let oP = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earthRadius + obj.avgAlt, 3) / GM));
 	// More stuff
})
```



So we set a variable with the above formula. We use `Math.round()` as the instructions wanted us to round to the nearest whole number. Next we can delete `avgAlt` as we have used it in the formula. Then we can add `oP ` to the object.



[MDN: delete operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete "MDN docs: delete")



```javascript
arr.map(obj => {
  let oP = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earthRadius + obj.avgAlt, 3) / GM));
  // Delete avgAlt
 	delete obj.avgAlt;
  // Add orbitalPeriod
  obj.orbitalPeriod = op;
})
```



We have updated the object(s) in `arr`. Lastly, we want to make sure to return `arr`.



# Our Solution

```javascript
function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  arr.map(obj => {
    let oP = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earthRadius + obj.avgAlt, 3) / GM));
    delete obj.avgAlt;
    obj.orbitalPeriod = oP;
  })
  return arr;
}
```



# Links & Resources

['Map the Debris' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris "Intermediate Algorithm Scripting: Map the Debris on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/037---map-the-debris.js "GitHub Solution")



Thank you for reading!