Challenge - 5 Problems
Path Resolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of path.resolve with relative and absolute paths?
Consider the following Node.js code using
path.resolve. What will be printed?Node.js
import path from 'path'; console.log(path.resolve('/foo', './bar', 'baz'));
Attempts:
2 left
💡 Hint
Remember that path.resolve normalizes and joins paths, resolving '.' and '..' segments.
✗ Incorrect
The path.resolve method joins the paths and normalizes them. The ./bar means 'bar' inside the current directory, so it becomes 'bar'. The final path is /foo/bar/baz.
❓ Predict Output
intermediate2:00remaining
What does path.resolve return when given an absolute path last?
What will this code output?
Node.js
import path from 'path'; console.log(path.resolve('foo', '/bar', 'baz'));
Attempts:
2 left
💡 Hint
When an absolute path appears, path.resolve resets the base to that path.
✗ Incorrect
When path.resolve encounters an absolute path, it discards previous segments and starts from that absolute path. Here, '/bar' is absolute, so the result is '/bar/baz'.
🔧 Debug
advanced2:30remaining
Why does this path.resolve call produce an unexpected path?
This code is intended to resolve to '/home/user/project/src/lib', but it outputs '/home/user/project/lib'. What is the cause?
Node.js
import path from 'path'; const base = '/home/user/project/src'; const result = path.resolve(base, '../lib'); console.log(result);
Attempts:
2 left
💡 Hint
Think about what '../lib' means relative to the base path.
✗ Incorrect
The segment '../lib' means go up one directory from 'src' and then into 'lib'. So the resolved path is '/home/user/project/lib'.
❓ component_behavior
advanced2:00remaining
How does path.resolve behave with empty strings in arguments?
What will this code output?
Node.js
import path from 'path'; console.log(path.resolve('', '/foo', '', 'bar'));
Attempts:
2 left
💡 Hint
Empty strings are ignored in path.resolve arguments.
✗ Incorrect
Empty strings are skipped. The absolute path '/foo' resets the base, and 'bar' is appended, resulting in '/foo/bar'.
📝 Syntax
expert2:30remaining
Which option causes a SyntaxError when importing and using path.resolve in Node.js ES modules?
Identify the code snippet that will cause a SyntaxError.
Attempts:
2 left
💡 Hint
Look for missing or extra characters that break syntax.
✗ Incorrect
Option C is missing a closing parenthesis in the console.log call, causing a SyntaxError.