0
0
Node.jsframework~20 mins

path.join for cross-platform paths in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'));
A'folder\\subfolder\\file.txt'
B'folder/subfolder/file.txt'
C'folder\\subfolder/file.txt'
D'folder/subfolder\\file.txt'
Attempts:
2 left
💡 Hint
Remember that path.join uses the platform-specific separator.
Predict Output
intermediate
2: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'));
A'/usr/local/bin'
B'/usr\\local\\bin'
C'/usr/local\\bin'
D'usr/local/bin'
Attempts:
2 left
💡 Hint
POSIX systems use forward slashes as path separators.
component_behavior
advanced
2: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'));
A'folder\\absolute\\file.txt'
B'folder\\/absolute\\file.txt'
C'folder/absolute/file.txt'
D'/absolute/file.txt'
Attempts:
2 left
💡 Hint
An absolute path in any argument resets the path from that point.
📝 Syntax
advanced
2: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'.
A
const path = require('path');
console.log(path.join('a', 'b'));
B
import * as path from 'path';
console.log(path.join('a', 'b'));
C
import path from 'path';
console.log(path.join('a', 'b'));
D
import { join } from 'path';
console.log(join('a', 'b'));
Attempts:
2 left
💡 Hint
ES modules use import syntax, not require.
🔧 Debug
expert
2: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));
A'parts' is an empty array, so output is an empty string.
B'path' module is not imported correctly.
C'parts' is null and cannot be spread, causing a TypeError.
D'path.join' does not accept spread arguments.
Attempts:
2 left
💡 Hint
Check the value and type of 'parts' before spreading.