Created
August 30, 2019 12:38
-
-
Save anjensan/20d462ddd2f3ce0da464b717a13226c3 to your computer and use it in GitHub Desktop.
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
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