# freeCodeCamp Challenge Guide: Map the Debris

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-map-the-debris/16021>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 8:37pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-map-the-debris/16021 "2016-06-30T20:37:59Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 8:37pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-map-the-debris/16021/1 "2016-06-30T20:37:59Z")

</div>

# [Map the Debris](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/map-the-debris)

* * *

## Problem Explanation

The first thing to do is to get familiar with what the program is for by knowing what Orbital period exactly is. You’ve to return a new array that transforms the element’s average altitude into their orbital periods. The parts generally found hard are finding the formula, implementing it and for some people, modifying objects by the key. However, something that is not very clear is the fact that your program has to be able to check for any number of objects in the array; This is what is tested on the second part.

#### Relevant Links

- [Orbital period](https://en.wikipedia.org/wiki/Orbital_period)
- [JS Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
- [Math.PI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/PI)
- [JS Math Pow](http://forum.freecodecamp.com/t/javascript-math-pow/14685)
- [Math.sqrt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt)
- [Math.round()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round)

* * *

## Hints

### Hint 1

The formula needed is:

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/3/b/3b3831f4334b255f143b3b1cdb0656f41bd008df.png)

### Hint 2

Use `Math.round()` to round up to the next whole number as requested. Using `Math.ceil()` will let you pass the first test but fail the second one.

### Hint 3

Find out how to remove and add key to a JavaScript object.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function orbitalPeriod(arr) {
> const GM = 398600.4418;
> const earthRadius = 6367.4447;
> const a = 2 * Math.PI;
> const newArr = [];
> 
> const getOrbPeriod = function(obj) {
> const c = Math.pow(earthRadius + obj.avgAlt, 3);
> const b = Math.sqrt(c / GM);
> const orbPeriod = Math.round(a * b);
> // create new object
> return {name: obj.name, orbitalPeriod: orbPeriod};
> };
> 
> for (let elem in arr) {
> newArr.push(getOrbPeriod(arr[elem]));
> }
> 
> return newArr;
> }
> 
> // test here
> orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }]);
> 
> ```
> 
> #### Code Explanation
> 
> - **GM** and **earthRadius** are both given to us.
> - To make the code easier to edit and read, each part of the equation is written separately.
> - Create **newArr** to store the `orbPeriod`’s.
> - **a** is 2 times pi. The part that is a constant is on the global scope while the rest is part of a function.
> - Create a function, `gerOrbPeriod()` that will do the required work for any amount of objects.
> - **c** is ( **earthRadius** + **avgAlt** ) to the cube.
> - **b** is the square root of **c** divided by **GM**.
> - Create **orbPeriod** to store the product of **a** and **b** , with the `Math.round()` function applied to round up to the next whole number.
> - Then we delete the key **avgAlt** , and add the new key and its value.
> 
> #### Relevant Links
> 
> - [JS For In Loop](http://forum.freecodecamp.com/t/javascript-for-in-loop/14665)
> - [JS Array Prototype Push](http://forum.freecodecamp.com/t/javascript-array-prototype-push/14298)

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function orbitalPeriod(arr) {
> const GM = 398600.4418;
> const earthRadius = 6367.4447;
> const newArr = [];
> 
> //Looping through each key in arr object
> for (let elem in arr) {
> //Rounding off the orbital period value
> const orbitalPer = Math.round(
> 2 * Math.PI * Math.sqrt(Math.pow(arr[elem].avgAlt + earthRadius, 3) / GM)
> );
> //Adding new object with orbitalPeriod property
> newArr.push({name: arr[elem].name, orbitalPeriod: orbitalPer});
> }
> 
> return newArr;
> }
> 
> // test here
> orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }]);
> 
> ```
> 
> #### Code Explanation
> 
> - **GM** and **earthRadius** are both given to us.
> - A `for..in` loop is used to iterate through each value in given array **arr**.
> - **orbitalPer** holds the value of orbital period for each iteration calculated using the formula.
> - The key **avgAlt** is deleted, and **orbitalPer** found is assigned in **arr**.

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> function orbitalPeriod(arr) {
> const GM = 398600.4418;
> const earthRadius = 6367.4447;
> // Create new array to prevent modification of the original
> const newArr = JSON.parse(JSON.stringify(arr));
> // Loop through each item in the array arr
> newArr.forEach(function(item) {
> // Calculate the Orbital period value
> const tmp = Math.round(
> 2 * Math.PI * Math.sqrt(Math.pow(earthRadius + item.avgAlt, 3) / GM)
> );
> //Delete the avgAlt property
> delete item.avgAlt;
> //Add orbitalPeriod property
> item.orbitalPeriod = tmp;
> });
> 
> return newArr;
> }
> 
> // test here
> orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }]);
> 
> ```
> 
> #### Code Explanation
> 
> - **GM** and **earthRadius** are both given to us.
> - The `forEach()` method is used to execute the function once per element ( **item** ) in **arr**.
> - **tmp** holds the value of orbital period for each element calculated using the formula.
> - The key **avgAlt** is deleted, and orbital period ( **tmp** ) found is assigned to the key **orbitalPeriod**.
> 
> #### Relevant Links
> 
> - [JS Array Prototype ForEach](http://forum.freecodecamp.com/t/javascript-array-prototype-foreach/14290)

> **Solution 4 (Click to Show/Hide)**
>
> ```javascript
> function orbitalPeriod(arr) {
> const GM = 398600.4418;
> const earthRadius = 6367.4447;
> return arr.map(({ name, avgAlt }) => {
> const earth = earthRadius + avgAlt;
> const orbitalPeriod = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earth, 3)/GM));
> return { name, orbitalPeriod };
> });
> }
> 
> orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
> 
> ```

---

_[View the full topic](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-map-the-debris/16021)._
