Student Activity

Student Activity (30-min)

Step-by-Step Procedure

  1. Write and Use a Custom Module to Calculate the Factorial of a Number (15 min):

    • Step 1: Create a file named factorial.js and 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.js
    • Expected Output:

      mathematica코드 복사Factorial of 5: 120
  2. Install a Third-Party Package like chalk and Use It to Color the Output (15 min):

    • Step 1: Install chalk:

      bash코드 복사npm install chalk
    • Step 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.js
    • Expected Output: The output will display "Factorial of 5:" in green and the result in blue.

Last updated