Challenge - 5 Problems
File Writing Mastery
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 file write operation?
Consider this Node.js code snippet that writes to a file asynchronously. What will be logged to the console?
Node.js
import { writeFile } from 'node:fs/promises'; async function writeData() { try { await writeFile('test.txt', 'Hello World'); console.log('Write successful'); } catch (err) { console.log('Write failed'); } } writeData();
Attempts:
2 left
💡 Hint
The writeFile function from 'fs/promises' returns a promise that resolves on success.
✗ Incorrect
The code uses the async writeFile function correctly with await inside a try-catch block. If the write succeeds, it logs 'Write successful'.
❓ component_behavior
intermediate2:00remaining
What happens if you try to write to a file without permissions?
Given this Node.js code snippet, what will be the console output if the process lacks write permission to 'readonly.txt'?
Node.js
import { writeFile } from 'node:fs/promises'; async function writeData() { try { await writeFile('readonly.txt', 'Data'); console.log('Write successful'); } catch (err) { console.log(err.code); } } writeData();
Attempts:
2 left
💡 Hint
EACCES is the error code for permission denied in Node.js.
✗ Incorrect
If the file is not writable due to permissions, writeFile throws an error with code 'EACCES'. The catch block logs this code.
📝 Syntax
advanced2:00remaining
Which option correctly appends text to a file asynchronously?
Select the code snippet that appends 'More data' to 'log.txt' using Node.js fs/promises module.
Attempts:
2 left
💡 Hint
Appending can be done by writeFile with flag 'a' or appendFile function.
✗ Incorrect
Option A correctly uses writeFile with flag 'a' to append asynchronously. Option B is also valid because appendFile is an async function that can be awaited inside an async function. Option C uses callback fs module without await. Option D uses appendFileSync incorrectly from promises module.
🔧 Debug
advanced2:00remaining
Why does this code throw an error when writing a file?
Identify the cause of the error in this Node.js code snippet:
Node.js
import fs from 'fs/promises'; fs.writeFile('output.txt', 'Hello').then(() => { console.log('Done'); }); fs.writeFile('output.txt', 'World');
Attempts:
2 left
💡 Hint
Promises must be handled to avoid unhandled rejections.
✗ Incorrect
The second writeFile call returns a promise that is not handled with await or then, which can cause unhandled promise rejection errors.
🧠 Conceptual
expert2:00remaining
What is the effect of using the 'wx' flag in writeFile options?
In Node.js fs/promises writeFile, what happens if you use the option { flag: 'wx' } when writing to a file?
Attempts:
2 left
💡 Hint
The 'x' flag means exclusive creation in file system flags.
✗ Incorrect
The 'wx' flag means write and exclusive create. It causes writeFile to fail if the file already exists, preventing overwriting.