Skip to content

Instantly share code, notes, and snippets.

@anjensan
Created August 30, 2019 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anjensan/20d462ddd2f3ce0da464b717a13226c3 to your computer and use it in GitHub Desktop.
Save anjensan/20d462ddd2f3ce0da464b717a13226c3 to your computer and use it in GitHub Desktop.
var SquareShape = (function(height){
var that = {
area : function () {
return height * height;
}
};
return that;
});
var RectShape = (function(width, height){
var that = {
area : function () {
return height * width;
}
};
return that;
});
var ComplexShape = (function(shapes) {
var that = {
area : function () {
return shapes.reduce(function(total, curr){
return total + curr.area();
}, 0);
}
};
return that;
});
function halvedAreaOf(s) {
return s.area() / 2;
};
function areaSqrtOf(s) {
return Math.sqrt(s.area());
};
var square = SquareShape(10);
var rect = RectShape(10, 20);
var complexShape = ComplexShape([square, rect]);
console.log(complexShape.area());
console.log(halvedAreaOf(complexShape));
console.log(areaSqrtOf(complexShape));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment