0
0
Node.jsframework~10 mins

path.parse and path.format in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to parse a file path into its parts using path.parse.

Node.js
const path = require('path');
const parsed = path.[1]('/home/user/file.txt');
console.log(parsed);
Drag options to blanks, or click blank then click option'
Abasename
Bformat
Cjoin
Dparse
Attempts:
3 left
💡 Hint
Common Mistakes
Using path.format instead of path.parse
Using path.basename which only returns the file name
2fill in blank
medium

Complete the code to create a path string from an object using path.format.

Node.js
const path = require('path');
const obj = { root: '/', dir: '/home/user', base: 'file.txt' };
const fullPath = path.[1](obj);
console.log(fullPath);
Drag options to blanks, or click blank then click option'
Aparse
Bformat
Cresolve
Dbasename
Attempts:
3 left
💡 Hint
Common Mistakes
Using path.parse which does the opposite
Using path.resolve which resolves absolute paths
3fill in blank
hard

Fix the error in the code to correctly parse the path '/var/log/sys.log'.

Node.js
const path = require('path');
const parts = path.[1]('/var/log/sys.log');
console.log(parts.ext);
Drag options to blanks, or click blank then click option'
Aparse
Bformat
Cjoin
Dbasename
Attempts:
3 left
💡 Hint
Common Mistakes
Using path.format which expects an object
Using path.basename which returns only the file name
4fill in blank
hard

Fill both blanks to parse a path and then format it back to a string.

Node.js
const path = require('path');
const parsed = path.[1]('/etc/nginx/nginx.conf');
const formatted = path.[2](parsed);
console.log(formatted);
Drag options to blanks, or click blank then click option'
Aparse
Bjoin
Cformat
Dbasename
Attempts:
3 left
💡 Hint
Common Mistakes
Using format before parse
Using join or basename which do different things
5fill in blank
hard

Fill all three blanks to parse a path, change its base, and format it back.

Node.js
const path = require('path');
const parsed = path.[1]('/usr/local/bin/script.sh');
parsed.base = '[2]';
parsed.name = '[3]';
const newPath = path.format(parsed);
console.log(newPath);
Drag options to blanks, or click blank then click option'
Aformat
Bscript_backup.sh
Cscript_backup
Dparse
Attempts:
3 left
💡 Hint
Common Mistakes
Changing base but not name
Using format instead of parse to get parts