0
0
Node.jsframework~15 mins

path.extname for file extensions in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Using path.extname to Extract File Extensions in Node.js
📖 Scenario: You are building a simple Node.js script to help organize files by their extensions. To do this, you need to extract the file extension from each filename.
🎯 Goal: Create a Node.js script that uses path.extname to get the file extension from a list of filenames.
📋 What You'll Learn
Create an array called files with the exact filenames: 'report.pdf', 'photo.jpg', 'archive.zip', 'notes.txt'
Create a variable called extToFind and set it to '.jpg'
Use a for loop with the variable file to iterate over files and create an array extensions that stores the extension of each file using path.extname(file)
Add a final line that exports the extensions array using module.exports = extensions
💡 Why This Matters
🌍 Real World
Extracting file extensions is useful when organizing files, validating uploads, or processing files differently based on type.
💼 Career
Understanding how to use Node.js built-in modules like path and how to manipulate arrays is essential for backend JavaScript development.
Progress0 / 4 steps
1
Create the list of filenames
Create an array called files with these exact filenames: 'report.pdf', 'photo.jpg', 'archive.zip', 'notes.txt'
Node.js
Need a hint?

Use square brackets [] to create an array and separate items with commas.

2
Add a variable for the extension to find
Create a variable called extToFind and set it to the string '.jpg'
Node.js
Need a hint?

Use const to declare a variable and assign the string '.jpg'.

3
Extract extensions using path.extname in a loop
Use a for loop with the variable file to iterate over files. Inside the loop, use path.extname(file) to get the extension and store all extensions in an array called extensions.
Node.js
Need a hint?

Remember to require('path') at the top. Use extensions.push(path.extname(file)) inside the loop.

4
Export the extensions array
Add a final line that exports the extensions array using module.exports = extensions
Node.js
Need a hint?

Use module.exports = extensions to make the array available outside this file.