2

I have a program that looks at two points and does operations with them. I need to always make sure that the points are in the proper order though. I need the first point to be the smaller of the two. I am trying to write a utility function that I can call inside of the other functions to reorder the arguments that were passed in. Any help in understanding why this isn't working would be amazing! Ill do my best to only post relevant code

var unionFind = {
  data: [],     //This will be filled by a later function
  reorderPoints: function(num1, num2) {
    // useful utility to make sure points are in proper order
    // we will always keep the smaller number to show if points
    // are connected.
    if(num1 > num2) {
      debugger;
      point1 = num2;
      point2 = num1;
    }
  },
  union: function(point1, point2) {
    this.reorderPoints(point1, point2);
    // if the two points are already connected
    // then just return true
    if(this.connected(point1, point2) === true) {
      return true;
    }
    // otherwise loop through the data array and
    // change all entries that are connected to
    // the first point to the value of the first
    // point
    for(var i = 0; i < this.data.length; i++) {
      if(this.data[i] === point2) {
        this.data[i] = point1;
      }
    }
  },

  connected: function() {
    this.reorderPoints(point1, point2);
    return this.data[point1] === this.data[point2];
  },
  initData: function(num) {
    for(var i = 0; i <= num; i++) {
      this.data[i] = i;
    }
  }
};

unionFind.initData(9);
console.log(unionFind.data);

unionFind.union(4,3);
console.log(unionFind.data);
1
  • Inner function is function defined inside another function - complementary, outer function is function in which this function defined. You literally don't have any of those in your code. What you may want is to pass arguments "by reference", but JavaScript doesn't support such blasphemy Commented Mar 9, 2016 at 22:33

1 Answer 1

2

In union method point1 and point2 are parameters, so they are local variables. When you enter reorderPoints method they do not exist there, so this code:

point1 = num2;
point2 = num1;

just creates new point1 and point2 variables (this time global ones because of no var before).

To solve this you would need to have point1 and point2 varibles declared in a namespace outside both those functions or you can construct an array of those two points and pass that array to the sort method, like this:

  reorderPoints: function(points) {
    if(points[0] > points[1]) {
      var tmp = points[0];
      points[0] = points[1];
      points[1] = tmp;
    }
  },
  union: function(point1, point2) {
    var points = [point1, point2];
    this.reorderPoints(points);
    // From this line you should use points[0] and points[1] as sorted points
    // because point1 and point2 parameters are not changed.
    (....)
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see I thought I was pulling off a closure here! But since the function isn't created in the outer function it doesn't have access to its variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.