# Map the Debris - treading verrry slowly

There is a lot going on here that I am trying to wrap my head around. In order to not confuse myself, I wanted to get the first testcase to pass before I make the code more robust for the 2nd (and subsequent) testcase(s).

When I run my current code in Atom/Chrome console, I get the following result (which I think is right):

FinalArray {name: “sputnik”, orbitalPeriod: 86400}

But in FCC, I get the following error:

`orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]) should return [{name: "sputnik", orbitalPeriod: 86400}].`

What am I missing?

Thank you,

``````function orbitalPeriod(arr) {
var GM = 398600.4418;

let name = arr[0]["name"];
let avgAlt = arr[0]["avgAlt"];

let firstCalc = Math.pow((earthRadius + avgAlt), 3);

//firstCalc/GM:
let secondCalc = firstCalc/GM;

//sqrt of secondCalc:
let thirdCalc = Math.sqrt(secondCalc);

//final result:
let finalResult = Math.round(thirdCalc * 2 * Math.PI);

//present final result:
function FinalArray(name, seconds){
this.name = name;
this.orbitalPeriod = seconds;
}

var myFinalArray = new FinalArray(name, finalResult);

console.log(myFinalArray);

}

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

``````

Looks like you’re returning the object – but the test case is checking the object in the first position of an array…

1 Like

Oh yes I see. OK.

So then I created an arr and tried to push final result object to that array but am getting very odd result: `1` (just the number `1`…).

New code:

``````function orbitalPeriod(arr) {
var GM = 398600.4418;

let finalArr = [];

let name = arr[0]["name"];
let avgAlt = arr[0]["avgAlt"];

let firstCalc = Math.pow((earthRadius + avgAlt), 3);

//firstCalc/GM:
let secondCalc = firstCalc/GM;

//sqrt of secondCalc:
let thirdCalc = Math.sqrt(secondCalc);

//final result:
let finalResult = Math.round(thirdCalc * 2 * Math.PI);

//present final result:
function FinalArray(name, seconds){
this.name = name;
this.orbitalPeriod = seconds;
}

var myFinalArray = new FinalArray(name, finalResult);

return finalArr.push(myFinalArray);

}

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

``````

So tell me – what does `finalArr.push(...)` actually return? Take a look at theMDN docs on Array.push(). That’s where your `1` is coming from.

You might want to do the `finalArr.push(...)` in one step, then `return finalArr` as a separate step.

1 Like

Thanks for all your help @snowmonkey.

My final code:

``````function orbitalPeriod(arr) {
var GM = 398600.4418;

let finalArr = [];

for(var i = 0; i < arr.length; i++){
let name = arr[i]["name"];
let avgAlt = arr[i]["avgAlt"];

let firstCalc = Math.pow((earthRadius + avgAlt), 3);

//firstCalc/GM:
let secondCalc = firstCalc/GM;

//sqrt of secondCalc:
let thirdCalc = Math.sqrt(secondCalc);

//final result:
let finalResult = Math.round(thirdCalc * 2 * Math.PI);
console.log("This is the final result: " + finalResult);

//present final result:
function FinalArray(name, seconds){
this.name = name;
this.orbitalPeriod = seconds;
}

var myFinalArray = new FinalArray(name, finalResult);

finalArr.push(myFinalArray);
};
return finalArr;
}

//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}].

``````

Great work, looks good!

Now, can you think of a way to do the same thing without a for loop? Have you learned about a function that operates on an array, and is passed a function to perform on each member of that array? One that returns a new array of the same length?

Not saying your answer is wrong, just that you can handle the same problem in many ways. But… If 98 job applicants use a for loop and two use Array.map(), i’m paying attention to those two.

1 Like

I will try it and get back to you.

1 Like