Complete the code to get the base file name from the path.
const path = require('path'); const filePath = '/home/user/docs/file.txt'; const baseName = path.[1](filePath); console.log(baseName);
The path.basename function returns the last part of a path, which is the file name.
Complete the code to get the directory name from the path.
const path = require('path'); const filePath = '/home/user/docs/file.txt'; const dirName = path.[1](filePath); console.log(dirName);
The path.dirname function returns the directory part of a path, excluding the file name.
Fix the error in the code to correctly get the base name of the file.
const path = require('path'); const filePath = '/var/log/system.log'; const base = path.[1](filePath, '.log'); console.log(base);
Use path.basename with the second argument to remove the extension from the file name.
Fill both blanks to create a dictionary with file names as keys and their directories as values.
const path = require('path'); const files = ['/a/b/c.txt', '/d/e/f.js', '/g/h/i.md']; const fileDirs = {}; for (const file of files) { fileDirs[path.[1](file)] = path.[2](file); } console.log(fileDirs);
Use basename to get the file name and dirname to get the directory for each file path.
Fill the blanks to create an object mapping file extensions to their base names.
const path = require('path'); const files = ['/x/y/z.py', '/a/b/c.js', '/m/n/o.py']; const extMap = {}; for (const file of files) { const ext = path.[1](file); const base = path.[2](file, ext); if (!extMap[ext]) { extMap[ext] = []; } extMap[ext].push(base); } console.log(extMap);
path.extname gets the file extension, path.basename gets the file name without the extension when passed the extension as second argument.