Skip to content

Instantly share code, notes, and snippets.

@dav1dddd
Created August 7, 2020 16:30
Show Gist options
  • Save dav1dddd/9f7452f5eb8e5927fd3ab143da77244f to your computer and use it in GitHub Desktop.
Save dav1dddd/9f7452f5eb8e5927fd3ab143da77244f to your computer and use it in GitHub Desktop.
class Triangle {
constructor(a, b, c, h) {
this.a = a;
this.b = b;
this.c = c;
this.h = h;
}
area() {
return `Area: ${this.b} * ${this.h} / 2 = ${(this.b * this.h) / 2}`;
}
perimeter() {
if ((this.a && this.c == null) || this.a == null || this.c == null) {
return;
} else {
return `Perimeter: ${this.a} + ${this.b} + ${this.c} = ${
this.a + this.b + this.c
}`;
}
}
}
class Rectangle {
constructor(h, w) {
this.h = h;
this.w = w;
}
area() {
return `Area: ${this.h} * ${this.w} = ${this.h * this.w}`;
}
perimeter() {
return `Perimeter: ${this.h} * 2 + ${this.w} * 2 = ${
this.h * 2 + this.w * 2
}`;
}
}
class Square extends Rectangle {
constructor(len) {
// The parents (Rectangle) class's constructor function is called for the Rectangles height and width.
// The super keyword is used here because the constructor for square is pretty much the same as rectangle.
super(len, len);
}
}
class Circle {
constructor(r) {
this.r = r;
}
radius() {
return `Radius: ${Math.PI} * ${this.r}² = ${Math.PI * this.r ** 2}`;
}
circumference() {
return `Circumference: ${Math.PI} * ${this.r} * 2 = ${
Math.PI * this.r * 2
}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment