Gerardo Leon
Gerardo Leon

Reputation: 71

javascript function distance between two points

i have been learning javascript for a few days. and im having problems with the sintaxis and the semantics of my programs, i can make run this simple problem. i dont know whats wrong with it

//2. **Distance between two points**. Create a 
//function that calculate the distance between two points 
//(every point have two coordinates: x, y). _HINT: Your function 
//Should receive four parameters_.


    function Point(x,y,x1,y1){
    this.x = x;
    this.y = y;
    this.x1 = x1;
    this.y1 = y1;

    this.distanceTo = function (point)
    {
        var distance = Math.sqrt((Math.pow(this.x1-this.x,2))+(Math.pow(this.y1-this.y,2)))
        return distance;
    };
}

var newPoint = new Point (10,100);
var nextPoint = new Point (25,5);


console.log(newPoint.distanceTo(nextPoint));

Upvotes: 3

Views: 12906

Answers (5)

ibrahim mahrir
ibrahim mahrir

Reputation: 31682

Point constructor should have just two arguments x and y. And distanceTo should use the x and y of this point ant the other point (the one passed as parametter).

function Point(x, y){ // only x and y
    this.x = x;
    this.y = y;

    this.distanceTo = function (point)
    {
        var dx = this.x - point.x;               // delta x
        var dy = this.y - point.y;               // delta y
        var dist = Math.sqrt(dx * dx + dy * dy); // distance
        return dist;
    };
}

var newPoint = new Point (10,100);
var nextPoint = new Point (25,5);


console.log(newPoint.distanceTo(nextPoint));

Note: Since all Point instances have the exact same distanceTo function, it is better to define it on the prototype instead of redefining it for each instance which will only increase creation time and waste a lot of ressources.

This is better:

function Point(x, y){ // only x and y
    this.x = x;
    this.y = y;
}

Point.prototype.distanceTo = function (point)
{
    var dx = this.x - point.x;               // delta x
    var dy = this.y - point.y;               // delta y
    var dist = Math.sqrt(dx * dx + dy * dy); // distance
    return dist;
};

var newPoint = new Point (10,100);
var nextPoint = new Point (25,5);


console.log(newPoint.distanceTo(nextPoint));

More about prototpes here!

Upvotes: 2

Jakub Chlebowicz
Jakub Chlebowicz

Reputation: 529

Your function function Point(x,y,x1,y1) gets four parameters, but you're declaring it with only two of them. At the distanceTo function you should relate to point which's parameter of your invoked function.

It should go like this; point.x gives you 'X' value of passed object.

@Edit: My solution for this "problem" is;

var Point = function (x,y) {
  this.x = x;
  this.y = y;

  this.distanceTo = function (point) {
    let calculations = Math.sqrt((Math.pow(point.x-this.x,2))+(Math.pow(point.y-this.y,2)));

    return calculations;
  }
}

var firstPoint = new Point(0,0);
var secPoint = new Point(2,2);

console.log(firstPoint.distanceTo(secPoint));

Upvotes: 0

Voncrumb
Voncrumb

Reputation: 146

Try This instead:

    function Point(x,y){
    this.x = x;
    this.y = y;
    

    this.distanceTo = function (point)
    {
        var distance = Math.sqrt((Math.pow(point.x-this.x,2))+(Math.pow(point.y-this.y,2)))
        return distance;
    };
}

var newPoint = new Point (10,100);
var nextPoint = new Point (20,25);

console.log(newPoint.distanceTo(nextPoint))

In your distanceTo function you needed to refer to point.x and point.y instead as those are the points of nextPoint.

Hope this Helped :3

Upvotes: 13

redeux
redeux

Reputation: 57

There's a few different ways to do this based on your code but since your function is expecting 4 inputs I went with that one.

 function Point(x,y,x1,y1){
        this.x = x;
        this.y = y;
        this.x1 = x1;
        this.y1 = y1;
        this.distanceTo = function() {
            return Math.sqrt((Math.pow(this.x1-this.x,2))+(Math.pow(this.y1-this.y,2)))
        };
}

var points = new Point (10,100,25,5);
console.log(points.distanceTo()

);

You also don't need to set the variable and then return it, you can just return the equation.

Upvotes: 0

Dave Something
Dave Something

Reputation: 46

You're applying the hint in the wrong place. It's the distanceTo function that should take four parameters. Given the hint, I wouldn't bother with the Point constructor (although I do like that thinking in general, it just doesn't seem to be what this question's looking for. Just go with distanceTo(x,y,x1,y1), and I don't think you'll have any trouble.

Upvotes: 0

Related Questions