Created
July 18, 2023 03:46
-
-
Save hsuan1117/aab6089eb151f1d6c0d807a5479caee7 to your computer and use it in GitHub Desktop.
Shape
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
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