Map The Debris - help

Tell us what’s happening:

Hello,

So I am having trouble understanding the formula to obtain the values of time orbitals. So far I have mapped the array of objects to extract the average altitude data and was looking to map again using the formula, however the values are not as they should be.

I presume the formula I am using is incorrect but I am not sure.

Thanks.

Your code so far


function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;

let results = arr.map(function(e) {
return e.avgAlt;
})

let orbitalPeriods = results.map(function (e) {
return 2 * Math.PI * Math.sqrt((earthRadius + e)/ GM)
})

console.log(orbitalPeriods)
}

orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Map the Debris

Link to the challenge:

you are using the wrong formula

you need to use this one:

in wikipedia it’s under “small body orbiting a central period”

SemiMajor (a3) = radiusEarth + avgAlt does it not? so which part of that formula is incorrect?

Thanks

Answered my own question, to the power of 3.

1 Like

After a few days off, I am back to tackling this challenge. So far, I have calculated the formula and deleted the avgAlt property. My thinking of how to add the new property would be to use for (let i = 0; i < arr.length; i++) { arr[i].orbitalPeriod = orbitalPeriods[0] however Im not sure how to iterate through the orbitalPeriods in order to add them. I tried using a nested loop with J but this did not work.

function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  let results = arr.map(function(e) {
return e.avgAlt;
})

let orbitalPeriods = results.map(function (e) {
return Math.round(2 * Math.PI * Math.sqrt(Math.pow((earthRadius + e),3)/ GM))
})

arr.map(function (e){
  delete e.avgAlt
})



for (let i = 0; i < arr.length; i++) {
  arr[i].orbitalPeriod = orbitalPeriods[0]
  
  console.log(arr[i])
  }

[/quote]

you don’t need a nested for loop, if you have the logic to have the result for when you have only one object in the array, you can use that logic when there is more than one object using a loop or map to apply it to each of them and build the array output

it seems you are trying to do it, but I suggest you put all the logic in only one map method

Solved! thanks for the help @ilenia

I did not realise you could put so much logic into a single map method, thats really good to know as I enjoy using map.

function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
 
 arr.map(function (e){
   let singleOrbital = Math.round(2 * Math.PI * Math.sqrt(Math.pow((earthRadius + e.avgAlt),3)/ GM))
   
   delete e.avgAlt;

   e.orbitalPeriod = singleOrbital

})

return arr;

  
}

please don’t use it like that

map returns a new array with each value generated by the callback function

if you don’t want to use the returned value you can use forEach

I will have to research into this as I don’t understand how it still passed the tests.

I thought because you stated put all logic into a single map method this would be correct, but I will change to forEach.

Thanks.

because the array contains objects, which are passed in by reference, so changing e you are changing the object inside arr

try to have a function without side effects
return arr.map(...)
make map returns an array of objects created there without changing the function input

example of something similar

let arr = [{name: "Marie", age: 15}, {name: "Thomas", age: 23}]

let newArr = arr.map(obj => ({name: obj.name, greeting: "Hi! How old are you? I'm " + obj.age + "!"}))

console.log(arr) //  [{name: "Marie", age: 15}, {name: "Thomas", age: 23}]
console.log(newArr) // [{name: "Marie", greeting: "Hi! How old are you? I'm 15!"}, {name: "Thomas", greeting: "Hi! How old are you? I'm 23!"}]
1 Like

Thanks for the information, i see you point.

So when using the map method is it important to create a new variable like you have done in your example with “arr” and “newArr”?

you don’t necessarily have to create a new variable

this can work too

I mean, if you still need to use the array in the function you need a new variable, but if you don’t need it don’t do it

1 Like