[Opinion wanted] Map the Debris using class

Please tell me what you think about the use of class in this challenge and these kind of problems. Should it be reserved for more OOP kind of approaches?

My code

function orbitalPeriod(arr) {
  let output=[];
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  /* Pay attention below */
  class SpaceThing {
    constructor(name,alt) {
      this.name = name;
      this.orbitalPeriod=Math.round(
        2*Math.PI*Math.sqrt(Math.pow(earthRadius+alt,3)/GM)
      );
    }
  }
  /* Pay attention above */

  /* This builds the answer array */
  for(let i=0;i<arr.length;i++) {
    output.push(new SpaceThing(arr[i].name,arr[i].avgAlt))
  }

  return output;
}

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

Thanks for your interest.

Challenge: Map the Debris

Link to the challenge:

To me it seems overkill. If you aren’t going to have any methods on that class, then what’s the point? I probably would just have a helper function called something like “createSpaceThing” that would return an object, basically doing what your constructor is doing.

1 Like