Student Activity
Student Activity (30-min)
Step-by-Step Procedure
Write and Use a Custom Module to Calculate the Factorial of a Number (15 min):
Step 1: Create a file named
factorial.jsand add:javascript코드 복사const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1)); module.exports = factorial;Step 2: Create another file named
app.js:javascript코드 복사const factorial = require('./factorial'); console.log('Factorial of 5:', factorial(5));Step 3: Run the application:
bash코드 복사node app.jsExpected Output:
mathematica코드 복사Factorial of 5: 120
Install a Third-Party Package like
chalkand Use It to Color the Output (15 min):Step 1: Install
chalk:bash코드 복사npm install chalkStep 2: Modify
app.js:javascript코드 복사const factorial = require('./factorial'); const chalk = require('chalk'); console.log(chalk.green('Factorial of 5:'), chalk.blue(factorial(5)));Step 3: Run the application:
bash코드 복사node app.jsExpected Output: The output will display "Factorial of 5:" in green and the result in blue.
Last updated