Challenge - 5 Problems
Path Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Node.js code using path.join?
Consider the following code snippet using
path.join on a Windows system. What will be the output printed?Node.js
import path from 'path'; console.log(path.join('folder', 'subfolder', 'file.txt'));
Attempts:
2 left
💡 Hint
Remember that path.join uses the platform-specific separator.
✗ Incorrect
On Windows, path.join uses backslashes \\ as separators. So the output joins the parts with \\.
❓ Predict Output
intermediate2:00remaining
What does path.join output on POSIX (Linux/macOS)?
Given this code running on a POSIX system, what is the output?
Node.js
import path from 'path'; console.log(path.join('/usr', 'local', 'bin'));
Attempts:
2 left
💡 Hint
POSIX systems use forward slashes as path separators.
✗ Incorrect
On POSIX systems, path.join uses forward slashes / to join paths.
❓ component_behavior
advanced2:00remaining
How does path.join handle absolute paths in arguments?
What will be the output of this code snippet?
Node.js
import path from 'path'; console.log(path.join('folder', '/absolute', 'file.txt'));
Attempts:
2 left
💡 Hint
An absolute path in any argument resets the path from that point.
✗ Incorrect
When path.join encounters an absolute path, it discards all previous segments and starts from the absolute path.
📝 Syntax
advanced2:00remaining
Which option correctly imports and uses path.join in ES modules?
Select the correct code snippet that imports
path and uses path.join to join 'a' and 'b'.Attempts:
2 left
💡 Hint
ES modules use import syntax, not require.
✗ Incorrect
In ES modules, import * as path from 'path' is the correct way to import the entire module. Option B uses CommonJS require which is invalid in ES modules. Option B imports the default export, but Node.js 'path' module does not have a default export. Option B imports only join which is valid if you want to use join directly.
🔧 Debug
expert2:00remaining
Why does this path.join code throw an error?
Examine the code below. Why does it throw a TypeError?
Node.js
import path from 'path'; const parts = null; console.log(path.join(...parts));
Attempts:
2 left
💡 Hint
Check the value and type of 'parts' before spreading.
✗ Incorrect
Spreading a null value causes a TypeError because null is not iterable. The error is not related to path.join or import.