0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
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.