Bird
Raised Fist0
Node.jsframework~20 mins

path.parse and path.format in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Path Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of path.parse on a Windows path
What is the output of the following Node.js code using path.parse on a Windows-style path?
Node.js
import path from 'path';
const parsed = path.parse('C:\\Users\\Alice\\Documents\\file.txt');
console.log(parsed);
A{ root: 'C:', dir: 'C:\\Users\\Alice\\Documents', base: 'file.txt', ext: '.txt', name: 'file' }
B{ root: 'C:\\', dir: 'C:\\Users\\Alice\\Documents', base: 'file.txt', ext: '.txt', name: 'file' }
C{ root: 'C:\\Users\\Alice\\Documents', dir: 'C:\\Users\\Alice', base: 'file.txt', ext: '.txt', name: 'file' }
D{ root: '', dir: 'C:\\Users\\Alice\\Documents', base: 'file.txt', ext: '.txt', name: 'file' }
Attempts:
2 left
💡 Hint
Remember that on Windows, the root includes the drive letter and backslash.
Predict Output
intermediate
2:00remaining
Result of path.format from a parsed object
Given the parsed path object below, what is the output of path.format?
Node.js
import path from 'path';
const parsed = {
  root: '/',
  dir: '/home/user/docs',
  base: 'notes.txt',
  ext: '.txt',
  name: 'notes'
};
console.log(path.format(parsed));
A/notes.txt
B/home/user/docs/.txt
C/home/user/docs/notes.txt
D/home/user/docs/notes
Attempts:
2 left
💡 Hint
The base property takes priority over name and ext when formatting.
component_behavior
advanced
2:00remaining
Effect of missing properties in path.format input
What will path.format output if the input object has only name and ext properties, but no dir or root?
Node.js
import path from 'path';
const obj = { name: 'image', ext: '.png' };
console.log(path.format(obj));
Aimage.png
B/image.png
C.png
Dimage
Attempts:
2 left
💡 Hint
If no directory or root is given, the output is just the file name.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in path.parse usage
Which option contains a syntax error when using path.parse in Node.js?
Aconst parsed = path.parse('/usr/local/bin'
Bconst parsed = path.parse('/usr/local/bin', true);
Cconst parsed = path.parse('/usr/local/bin'); console.log(parsed);
Dconst parsed = path.parse('/usr/local/bin');
Attempts:
2 left
💡 Hint
Check for missing parentheses or semicolons.
🔧 Debug
expert
3:00remaining
Why does path.format ignore name and ext when base is present?
Consider this code snippet:
const obj = { dir: '/var/log', base: 'app.log', name: 'app', ext: '.log' };
console.log(path.format(obj));
Why does the output use base and ignore name and ext?
ABecause <code>base</code> is empty, so <code>name</code> and <code>ext</code> are ignored.
BBecause <code>name</code> and <code>ext</code> are deprecated properties and ignored by <code>path.format</code>.
CBecause <code>path.format</code> only uses <code>dir</code> and <code>base</code> and never uses <code>name</code> or <code>ext</code>.
DBecause <code>base</code> has higher priority and overrides <code>name</code> and <code>ext</code> when formatting the path.
Attempts:
2 left
💡 Hint
Check the priority order of properties in path.format.

Practice

(1/5)
1. What does path.parse do in Node.js?
easy
A. It breaks a file path into parts like root, dir, base, name, and ext.
B. It combines multiple file paths into one string.
C. It reads the contents of a file at the given path.
D. It deletes a file at the specified path.

Solution

  1. Step 1: Understand the purpose of path.parse

    path.parse takes a file path string and splits it into an object with properties like root, dir, base, name, and ext.
  2. Step 2: Compare with other options

    Options A, C, and D describe different file system operations, not path parsing.
  3. Final Answer:

    It breaks a file path into parts like root, dir, base, name, and ext. -> Option A
  4. Quick Check:

    path.parse splits path into parts [OK]
Hint: Remember: parse means split path into pieces [OK]
Common Mistakes:
  • Confusing path.parse with reading file contents
  • Thinking path.parse combines paths
  • Mixing up path.parse with deleting files
2. Which of the following is the correct way to use path.format to build a path from parts?
easy
A. path.format('file.txt')
B. path.format('/home/user/file.txt')
C. path.format({ root: '/', dir: '/home/user', base: 'file.txt' })
D. path.format(['home', 'user', 'file.txt'])

Solution

  1. Step 1: Identify correct argument type for path.format

    path.format expects an object with path parts like root, dir, base, name, or ext.
  2. Step 2: Check each option

    path.format({ root: '/', dir: '/home/user', base: 'file.txt' }) correctly passes an object with root, dir, and base. Options B, C, and D pass strings or arrays, which are invalid.
  3. Final Answer:

    path.format({ root: '/', dir: '/home/user', base: 'file.txt' }) -> Option C
  4. Quick Check:

    path.format needs object with parts [OK]
Hint: path.format needs an object, not a string or array [OK]
Common Mistakes:
  • Passing a string instead of an object to path.format
  • Using an array instead of an object
  • Confusing path.format with path.parse usage
3. What is the output of this code?
const path = require('path');
const parsed = path.parse('/home/user/docs/file.txt');
console.log(parsed.base);
medium
A. '/home/user/docs/file.txt'
B. 'file.txt'
C. 'file'
D. '.txt'

Solution

  1. Step 1: Understand what path.parse returns

    path.parse returns an object with properties including base, which is the last part of the path with extension.
  2. Step 2: Check the base property for given path

    For '/home/user/docs/file.txt', base is 'file.txt'.
  3. Final Answer:

    'file.txt' -> Option B
  4. Quick Check:

    parsed.base = 'file.txt' [OK]
Hint: base is filename with extension from path.parse [OK]
Common Mistakes:
  • Confusing base with dir or name
  • Expecting full path instead of base
  • Mixing base with extension only
4. Identify the error in this code snippet:
const path = require('path');
const parts = path.parse('/var/log/sys.log');
const newPath = path.format(parts.dir + '/backup/' + parts.base);
console.log(newPath);
medium
A. path.format expects an object, but a string was passed.
B. path.parse cannot parse absolute paths.
C. The variable parts is not defined.
D. The console.log statement is missing parentheses.

Solution

  1. Step 1: Check the argument passed to path.format

    path.format requires an object with path parts, but here a string is passed by concatenating parts.dir, '/backup/', and parts.base.
  2. Step 2: Understand correct usage of path.format

    To add '/backup/' folder, modify parts.dir property or create a new object, then pass that object to path.format.
  3. Final Answer:

    path.format expects an object, but a string was passed. -> Option A
  4. Quick Check:

    path.format needs object, not string [OK]
Hint: path.format always needs an object, never a string [OK]
Common Mistakes:
  • Passing a string instead of an object to path.format
  • Assuming path.parse fails on absolute paths
  • Forgetting parentheses in console.log (not true here)
5. Given this code, what will console.log(newPath) output?
const path = require('path');
const parts = path.parse('/usr/local/bin/node');
const updatedParts = { ...parts, dir: parts.dir + '/backup' };
const newPath = path.format(updatedParts);
console.log(newPath);
hard
A. '/usr/local/bin/backup/'
B. '/usr/local/bin/node/backup'
C. '/usr/local/bin/node'
D. '/usr/local/bin/backup/node'

Solution

  1. Step 1: Analyze how updatedParts modifies dir

    updatedParts copies all parts but changes dir to parts.dir + '/backup', so dir becomes '/usr/local/bin/backup'.
  2. Step 2: Understand path.format output

    path.format builds path from updatedParts, combining dir and base ('node'), resulting in '/usr/local/bin/backup/node'.
  3. Final Answer:

    '/usr/local/bin/backup/node' -> Option D
  4. Quick Check:

    Modified dir + base = '/usr/local/bin/backup/node' [OK]
Hint: Changing dir in parts changes folder path in output [OK]
Common Mistakes:
  • Appending backup to base instead of dir
  • Confusing order of path parts
  • Forgetting to spread parts before modifying