Homework & Coding Practice
Homework/Practice Coding Assignment (20-min)
Write a Module That Exports Utility Functions for String Manipulation
Steps:
Step 1: Create a directory named
string-utilsand initialize it:bash코드 복사mkdir string-utils cd string-utils npm init -yStep 2: Create a file named
stringUtils.jswith 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 };Step 3: Create another file
app.jsto 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));Step 4: Run the application:
bash코드 복사node app.jsExpected Output:
makefile코드 복사Original: Hello World Reversed: dlroW olleH Uppercase: HELLO WORLD Lowercase: hello worldStep 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
Students understand how to create and use custom modules with CommonJS and ES6 syntax.
Students can differentiate between core and third-party modules.
Homework reinforces creating modular and reusable code.
Last updated