Skip to content

Instantly share code, notes, and snippets.

@hsuan1117
Created July 18, 2023 03:46
Show Gist options
  • Save hsuan1117/aab6089eb151f1d6c0d807a5479caee7 to your computer and use it in GitHub Desktop.
Save hsuan1117/aab6089eb151f1d6c0d807a5479caee7 to your computer and use it in GitHub Desktop.
Shape
class Shape {
constructor(type, values) {
this.type = type
this.values = values
}
getArea() {
let area = 0
switch(this.type) {
case "Rectangle":
area = this.values[0] * this.values[1]
break;
case "Triangle":
const s = (this.values[0] + this.values[1] + this.values[2]) / 2
area = Math.sqrt(
s * (s-this.values[0]) * (s-this.values[1]) * (s-this.values[2])
)
break;
}
return area.toFixed(2)
}
getPerimeter() {
let per = 0
switch(this.type) {
case "Rectangle":
per = (this.values[0] + this.values[1])*2
break;
case "Triangle":
per = (this.values[0] + this.values[1] + this.values[2])
break;
}
return per.toFixed(2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment