- Create a new folder.
- Add a
main.js
file to the folder.
- Add a
math-module.js
file to your folder - In the
math-module.js
file add a function calledsum
. Thesum
function should take two arguments and return their sum.
Example:
sum(3, 6); //returns 9
- Add a
module.exports
statement at the bottom of themath-module.js
file. - Add the
sum
function tomodule.exports
. - In the
main.js
file, use therequire
keyword to import themath-module.js
file. - Call the
sum
function from themain.js
file and save the result to a new variable. - Add a
console.log
statement that logs the saved variable. - Open the terminal and run the
main.js
file. You should see the output from thesum
function - Add three more functions to the
math.module.js
file:
- A
multiply
function (takes two arguments and returns their product). - A
divide
function (takes two arguments and returns the first argument divided by the second). - A
square
function (takes one argument and returns its square).
Examples:
multiply(2, 5); //returns 10
divide(20, 10); //returns 2
square(5); //returns 25
- Add the
multiply
,divide
, andsquare
functions tomodule.exports
in themath-module.js
file. - In the
main.js
file, use the use the importedmath-module.js
file to call the three new functions, and save the results as new variables - Log the saved variable.
- Open the terminal and run the
main.js
file. You should see the output from all the functions.
- Create a new file called
strings-module
that contains at least three string functions (for example: return the first letter of a string, reverse a string, etc.) of the choosing. - Import the
string-module
into themain.js
file and try calling and logging the functions fromstring-module
. - Can you also import the
string-module
into themath-module
and use it in there? Or vice versa?
The following is a code for an program that takes a user's input of a folder and an extension, and lists all the files in that folder that have the given extension.
const fs = require('fs')
const path = require('path')
const folder = process.argv[2]
const ext = '.' + process.argv[3]
fs.readdir(folder, (err, files) => {
if (err) {
console.error(err);
return;
}
files.forEach((file) => {
if (path.extname(file) === ext) {
console.log(file)
}
})
})
In a new folder, create the files filterFiles.js
and main.js
.
- Paste the above code into
filterFiles.js
and rewrite it as follows:
- Do not take any input from the user (i.e. remove lines that make use of the
process
object). - Wrap the code in a function that takes as arguments a folder name, an extension, and a callback function.
- If there is an error while reading the folder, invoke the callback with the error.
- Otherwise, invoke the callback at the bottom of the function with two arguments:
null
and the list of files. - Export the function using
module.exports
.
- In
main.js
do the following:
- Import the function from
filterFiles.js
asfilterFilesFn
. - Read the input for folder and extension from the user (using the
process
object). - invoke the function with the following arguments:
- the folder
- the extension
- a callback function that takes as arguments an error object and a list. If the error object is not
null
, it logs the string:'there was an error'
followed by the erro. Otherwise, it loges the list, with each element in a separate line.