-
-
Save TimBlock/e60548ea0625c70c5902 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your code here. | |
function Vector(x,y){ | |
this.x=x; | |
this.y=y; | |
}; | |
Vector.prototype.plus = function(vector){ | |
return new Vector(this.x+vector.x, this.y+vector.y) | |
} | |
Vector.prototype.minus = function(vector){ | |
return new Vector(this.x-vector.x, this.y - vector.y) | |
} | |
Object.defineProperty(Vector.prototype, "length",{get: function(){ | |
var x = Math.pow(this.x, 2); | |
var y = Math.pow(this.y, 2); | |
return Math.sqrt(x+y); | |
} | |
}); | |
console.log(new Vector(1, 2).plus(new Vector(2, 3))); | |
// → Vector{x: 3, y: 5} | |
console.log(new Vector(1, 2).minus(new Vector(2, 3))); | |
// → Vector{x: -1, y: -1} | |
console.log(new Vector(3, 4).length); | |
// → 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment