0

A have a question about JavaScript.

Why this works:

function orbitalPeriod(arr) {
   
   function orbitalPeriodInside(inputData) {
    const GM = 398600.4418;
    const earthRadius = 6367.4447;
    let averageAltitude = inputData.avgAlt;
    let myNewObj = {};

    let upperPartBeforePow = earthRadius + averageAltitude;
    let upperPartAfterPow = Math.pow(upperPartBeforePow, 3);
    let dividedByLowerPart = upperPartAfterPow / GM;
    let sqrt = Math.sqrt(dividedByLowerPart);
    let orbitalPeriodT = 2 * 3.14159265359 * sqrt;
    let orbitalPeriodTRound = Math.round(orbitalPeriodT);
  
    myNewObj.name = inputData.name;
    myNewObj.orbitalPeriod = orbitalPeriodTRound;

    return myNewObj;
  }
 
  let results = arr.map(function(oneObj) {
    return orbitalPeriodInside(oneObj);
  });
  console.log(results);
  return results;
}

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

And why this doesn't:

    function orbitalPeriod(arr) {
      
       let results = [];
       
       function orbitalPeriodInside(inputData) {
        const GM = 398600.4418;
        const earthRadius = 6367.4447;
        let averageAltitude = inputData.avgAlt;
        let myNewObj = {};

        let upperPartBeforePow = earthRadius + averageAltitude;
        let upperPartAfterPow = Math.pow(upperPartBeforePow, 3);
        let dividedByLowerPart = upperPartAfterPow / GM;
        let sqrt = Math.sqrt(dividedByLowerPart);
        let orbitalPeriodT = 2 * 3.14159265359 * sqrt;
        let orbitalPeriodTRound = Math.round(orbitalPeriodT);
      
        myNewObj.name = inputData.name;
        myNewObj.orbitalPeriod = orbitalPeriodTRound;

        return myNewObj;
      }
     
      results = arr.map(function(oneObj) {
        return orbitalPeriodInside(oneObj);
      });
      console.log(results);
      return results;
    }

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

The only difference is declaration of 'let results'. Second example - the declaration is in top of the function but it won't work. This second function returns only 'use strict'. Why?

John Taylor
  • 729
  • 3
  • 11
  • 22
  • They both look like they return the same thing in those snippets – Jaromanda X Dec 02 '16 at 03:51
  • What browser are you using? (@JohnTaylor) – asemahle Dec 02 '16 at 03:52
  • `This second function returns only 'use strict'. Why?` as the term `use strict` is never mentioned in either snippet, I think the problem is with code you haven't shown, or some other misunderstanding on your part – Jaromanda X Dec 02 '16 at 03:52
  • Ok, now it is working! I restarted the browser. It was problem with old session on https://repl.it/. By the way - is there diffrence between this two examples. Is it better to declare let results = []; in the top, or it doesn't matter? – John Taylor Dec 02 '16 at 03:55
  • @JohnTaylor Did you add `'use strict';` in both functions? Did the linked question helped you to solve problem? – Tushar Dec 02 '16 at 03:56

0 Answers0