Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* shape-area.js */
- const PI = Math.PI;
- // declare and export two functions
- function circleArea(r) {
- return PI * r * r;
- }
- function squareArea(side) {
- return side * side;
- }
- // then add them as properties to the built-in module.exports object, to make these functions available to other files
- module.exports.circleArea = circleArea;
- module.exports.squareArea = squareArea;
- console.log(circleArea(5));
- console.log(squareArea(5));
- --------------------------------------------------------------------------------------------------------------
- /* app.js */
- const radius = 5;
- const sideLength = 10;
- // Option 1: import the entire shape-area.js module here (file path, file name & code from above)
- const areaFunctions = require('./shape-area.js');
- // Option 2: import circleArea and squareArea with object destructuring - to extract only the needed functions.
- //const { circleArea, squareArea } = require("./shape-area.js")
- // use the imported .circleArea() and .squareArea() methods here
- const areaOfCircle = areaFunctions.circleArea(radius); //then can use the method imported from the file here
- const areaOfSquare = areaFunctions.squareArea(sideLength);
- console.log(areaOfCircle);
- console.log(areaOfSquare);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement