0

i got a problem - when i update the "distanz" of my object "Punkt" it is not being changed, so how can i update the attributes of the object?

I also have the problem that i can't figure out how to delete the object that is 100% similar to "AE_punkt" - i would like to know how to remove it out of the array.

Thanks for all upcomming help!

function Punkt(name, x, y, check, distanz) {
  this.name = name;
  this.x = x; //Längengrad
  this.y = y; //Breitengrad
  this.check = check;
  this.distanz = distanz;
}

function getDistanz(x1, y1, x2, y2) {
  var y = 111.3 * (y1 - y2); //111.3 = Abstand zwischen zwei Breitenkreisen in km
  var x = 71.5 * (x1 - x2); //71.5 = Abstand zwischen zwei Längenkreisen in km
  return Math.sqrt((x * x) + (y * y));
}

function compare(a, b) {
  if (a < b) {
    return true;
  }
}

function nearestNeighbour() {
  var array = new Array();

  // in Array automatisch einfügen
  // Breiten- und Längengrad von https://www.laengengrad-breitengrad.de/
  var Dresden = new Punkt("Dresden", 13.7372621, 51.0504088, false, 0);
  array.push(Dresden);
  var Muenchen = new Punkt("München", 11.581981, 48.135125, false, 0);
  array.push(Muenchen);
  var Berlin = new Punkt("Berlin", 13.404954, 52.520007, false, 0);
  array.push(Berlin);
  var AE_punkt = new Punkt("Berlin", 13.404954, 52.520007, false, 0);

  //while schleife am besten
  var min = 9999999999999999999999999999;

  for (var i = 0; i < array.length; i++) {
    array[i].distanz = this.getDistanz(AE_punkt.x, AE_punkt.y, array[i].x, array[i].y);
    console.log(array[i].distanz);

    if (array[i].distanz != 0) {
      if (compare(array[i].distanz, min)) {
        min = array[i].distanz;
        console.log("Min:" + min);
      }
    }
  }

  for (var i = 0; i < array.length; i++) {
    if (array[i].distanz = 0) {
      array.splice(i, 1); //entfernt den Punkt der dem aktuellen AE_punkt entspricht
    }
    console.log(array[i]);
  }
}

nearestNeighbour();
RobG
  • 142,382
  • 31
  • 172
  • 209
saltea
  • 37
  • 6
  • 3
    you are missing equal sign it should be `==` or `===` in `if (array[i].distanz = 0) ` – hawks Feb 17 '20 at 11:00
  • @ajuni880 thanks, didnt noticed that! But why did that update the attributes too? – saltea Feb 17 '20 at 11:18
  • 1
    because you are updating the value with the assignment `=` operator here in your code `array[i].distanz = 0`. And then this is evaluated to a falsy value because of the `0` you are assigning to the property, thus the condition is not met and therefore the code is not executed in the if block – hawks Feb 17 '20 at 11:27

0 Answers0