Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yitonghe00/01f6fa5d9344f57fa91cd633ebcfadea to your computer and use it in GitHub Desktop.
Save yitonghe00/01f6fa5d9344f57fa91cd633ebcfadea to your computer and use it in GitHub Desktop.
// Your code here.
class Vec {
constructor(x, y) {
this.x = x;
this.y = y;
}
plus(parameter) {
return new Vec(this.x + parameter.x, this.y + parameter.y);
}
minus(parameter) {
return new Vec(this.x - parameter.x, this.y - parameter.y);
}
get length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
console.log(new Vec(1, 2).plus(new Vec(2, 3)));
// → Vec{x: 3, y: 5}
console.log(new Vec(1, 2).minus(new Vec(2, 3)));
// → Vec{x: -1, y: -1}
console.log(new Vec(3, 4).length);
// → 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment