Homework & Coding Practice

Homework/Practice Coding Assignment (20-min)

Write a Module That Exports Utility Functions for String Manipulation

Steps:

  1. Step 1: Create a directory named string-utils and initialize it:

    bash코드 복사mkdir string-utils
    cd string-utils
    npm init -y
  2. Step 2: Create a file named stringUtils.js with utility functions:

    javascript코드 복사const reverseString = (str) => str.split('').reverse().join('');
    const toUpperCase = (str) => str.toUpperCase();
    const toLowerCase = (str) => str.toLowerCase();
    
    module.exports = { reverseString, toUpperCase, toLowerCase };
  3. Step 3: Create another file app.js to test the module:

    javascript코드 복사const { reverseString, toUpperCase, toLowerCase } = require('./stringUtils');
    
    const testString = 'Hello World';
    console.log('Original:', testString);
    console.log('Reversed:', reverseString(testString));
    console.log('Uppercase:', toUpperCase(testString));
    console.log('Lowercase:', toLowerCase(testString));
  4. Step 4: Run the application:

    bash코드 복사node app.js
  5. Expected Output:

    makefile코드 복사Original: Hello World
    Reversed: dlroW olleH
    Uppercase: HELLO WORLD
    Lowercase: hello world
  6. Step 5: Experiment with adding a new function, such as capitalize, and test it:

    javascript코드 복사const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
    module.exports = { reverseString, toUpperCase, toLowerCase, capitalize };

Expected Outcomes

  1. Students understand how to create and use custom modules with CommonJS and ES6 syntax.

  2. Students can differentiate between core and third-party modules.

  3. Homework reinforces creating modular and reusable code.

Last updated