Skip to content
Advertisement

Unable to run a function in an object and print a new array – a bit confused

const ancientWonders = {
  // name of the list
  name: "The Ancient Wonders of the World",
  // list of locations
  list: [
    [29.97916667, 31.13416667, "Great Pyramid of Giza"],
    [33.5355, 44.2475, "Hanging Gardens of Babylon"],
    [37.63777778, 21.63, "Statue of Zeus at Olympia"],
    [37.94972222, 27.36388889, "Temple of Artemis at Ephesus"],
    [37.03777778, 27.42416667, "Mausoleum at Halicarnassus"],
    [36.4511, 28.2278, "Colossus Rhodes"],
    [31.21388889, 29.88555556, "Lighthouse of Alexandria"],
  ],
  // to store all distances between points
  distances: [],
  // to store shortest trip in list
  shortest: [],
  // to store longest trip in list
  longest: [],
  // function to convert above coordinatates into radians
  calcRad: function () {
    for (let i = 0; i < this.list.length; i++) {
      for (let c = 0; c < this.list[i].length; c++) {
        if (typeof this.list[i][c] !== "number") continue;
        const rad = (this.list[i][c] * Math.PI) / 180;
        return rad;
      }
    }
    this.distances.push(rad);
  },
  // to store direct route between points
  directRoute: [],
};

console.log(ancientWonders.calcRad(ancientWonders.distances));

For some reason, it only calculates the first coordinate of array 1 in the console..

could you please let me know what I’m doing wrong here?

Advertisement

Answer

Firstly no need to pass an arugment to calcRad. You are not using any in your method.

You are returning from inside the loop and that is why it is not run for all the items. Infact, your distances array is still empty and you just returned the first value. You need to move the return statement outside the loop, so after all the items are done, then you can return a suitable value.

Also, you need to run the loop for all the items while pushing. So have to move the push statement inside the first for loop.

const ancientWonders = {
  // name of the list
  name: "The Ancient Wonders of the World",
  // list of locations
  list: [
    [29.97916667, 31.13416667, "Great Pyramid of Giza"],
    [33.5355, 44.2475, "Hanging Gardens of Babylon"],
    [37.63777778, 21.63, "Statue of Zeus at Olympia"],
    [37.94972222, 27.36388889, "Temple of Artemis at Ephesus"],
    [37.03777778, 27.42416667, "Mausoleum at Halicarnassus"],
    [36.4511, 28.2278, "Colossus Rhodes"],
    [31.21388889, 29.88555556, "Lighthouse of Alexandria"],
  ],
  // to store all distances between points
  distances: [],
  // to store shortest trip in list
  shortest: [],
  // to store longest trip in list
  longest: [],
  // function to convert above coordinatates into radians
  calcRad: function () {
    for (let i = 0; i < this.list.length; i++) { 
      let rad = null;
      for (let c = 0; c < this.list[i].length; c++) {
        if (typeof this.list[i][c] !== "number") continue;
        rad = (this.list[i][c] * Math.PI) / 180;
      }
      this.distances.push(rad);
    }
    return this.distances;
  },
  // to store direct route between points
  directRoute: [],
};

console.log(ancientWonders.calcRad());
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement