Published
Edited
Oct 31, 2021
2 forks
Insert cell
Insert cell
Insert cell
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}

equals(point) {
return this.x === point.x && this.y === point.y;
}

clone() {
return new Point(this.x, this.y);
}

isZero() {
return this.x === 0 && this.y === 0;
}

angle() {
return Math.atan2(this.y, this.x);
}

length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}

add(point) {
return new Point(this.x + point.x, this.y + point.y);
}

substract(point) {
return new Point(this.x - point.x, this.y - point.y);
}
}
Insert cell
Insert cell
{
let message = "";
const PA = new Point(1, 2);

// Equal
if (PA.equals(new Point(1, 2)) !== true) {
message += `Equals: PA.equals(new Point(1, 2)) !== true test failed. \n`;
}

if (PA.equals(new Point(1, 1)) !== false) {
message += `Equals: PA.equals(new Point(1, 1)) !== false test failed. \n`;
}

// Clone
if (PA.equals(PA.clone(new Point(1, 2))) !== true) {
message += `Clone: PA.equals(PA.clone(new Point(1, 2))) !== true test failed. \n`;
}

// Length
if (PA.length() !== Math.sqrt(5)) {
message += `Length: PA.length() !== Math.sqrt(5) test failed. \n`;
}

// Succeed?
if (message === "") {
message = "Succeed";
}

return md`${message}`;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more