Created
April 18, 2018 19:22
-
-
Save kudapara/efc3e27411fae9bc08945a34a5a5a780 to your computer and use it in GitHub Desktop.
Code that voilates OCP
This file contains 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 AreaCalculator { | |
constructor (shapes) { | |
this.shapes = shapes | |
} | |
sum () { | |
return this.shapes | |
.map((shape) => { | |
if (shape instanceof Circle) { | |
return 2 * Math.PI * Math.pow(shape.radius, 2) | |
} | |
else if (shape instanceof Square) { | |
return Math.pow(shape.length, 2) | |
} | |
}) | |
.reduce((totalArea, currentArea) => { | |
return totalArea + currentArea | |
}, 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment