Skip to content

Instantly share code, notes, and snippets.

@simrandotdev
Created October 7, 2019 15:12
Show Gist options
  • Select an option

  • Save simrandotdev/c3bb013f20c930fd512c60a9cb8e3b65 to your computer and use it in GitHub Desktop.

Select an option

Save simrandotdev/c3bb013f20c930fd512c60a9cb8e3b65 to your computer and use it in GitHub Desktop.
Shape.js
function Shape(){
let shape = [];
//shape.totalVolume = totalVolume;
/* Added the function add the shape in the 'shape' array. */
shape.addShape = function (newShape) {
shape.push(newShape);
}
//console.log(shape2.computeConeVol(5,10));
shape.volume = function () {
/* Instead of declaring a array, declaring a variable with initial value 0 */
let totalVolume = 0.0;
shape.forEach(item => {
/* Adding the volumes of shapes into the totalVolume variable. */
totalVolume = totalVolume + item.computeVolume();
});
return totalVolume;
}
return shape;
}
let Sphere = require ('./sphere');
let shape1 = new Sphere(8);
let Cone = require ('./cone');
let shape2 = new Cone(5,10);
/* Created the shape object and added objects to it */
let shapes = Shape();
shapes.addShape(shape1);
shapes.addShape(shape2);
/* Calling the volume function of the Shape class. */
console.log(shapes.volume());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment