Challenge - 5 Problems
File Append Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Appending text to a file with fs.appendFile
What will be the content of the file 'log.txt' after running this code if the file initially contains 'Hello'? Assume no errors occur.
Node.js
import { appendFileSync, readFileSync } from 'node:fs'; appendFileSync('log.txt', ' World'); const content = readFileSync('log.txt', 'utf8'); console.log(content);
Attempts:
2 left
💡 Hint
Appending adds new content to the end of the existing file content.
✗ Incorrect
The appendFileSync method adds the string ' World' to the end of the existing file content 'Hello', resulting in 'Hello World'.
❓ Predict Output
intermediate2:00remaining
Asynchronous file append behavior
What will be logged to the console after running this code snippet?
Node.js
import { appendFile } from 'node:fs'; appendFile('notes.txt', 'New note\n', (err) => { if (err) throw err; console.log('Note added'); }); console.log('Appending started');
Attempts:
2 left
💡 Hint
Asynchronous functions run their callback after the main thread continues.
✗ Incorrect
The console.log('Appending started') runs immediately. The callback logs 'Note added' after the append operation finishes asynchronously.
🔧 Debug
advanced2:00remaining
Identifying error in appending to a file
This code is intended to append 'Data' to 'file.txt'. What error will it raise when run?
Node.js
import fs from 'node:fs'; fs.appendFile('file.txt', 'Data', (error) => { if (error) throw error; }); fs.appendFileSync('file.txt', 'More data')
Attempts:
2 left
💡 Hint
Check the usage of appendFile and appendFileSync methods.
✗ Incorrect
Both appendFile and appendFileSync are valid methods. The code correctly uses a callback for appendFile and a synchronous call for appendFileSync. No error occurs.
❓ component_behavior
advanced2:00remaining
Effect of encoding on appending files
What will be the content of 'data.txt' after running this code if the file initially contains 'abc'?
Node.js
import { appendFileSync, readFileSync } from 'node:fs'; appendFileSync('data.txt', Buffer.from([0x20, 0x64, 0x65, 0x66])); const content = readFileSync('data.txt', 'utf8'); console.log(content);
Attempts:
2 left
💡 Hint
Buffer bytes correspond to ASCII characters; 0x20 is a space.
✗ Incorrect
The buffer bytes [0x20, 0x64, 0x65, 0x66] correspond to ' def'. Appending this to 'abc' results in 'abc def'.
📝 Syntax
expert3:00remaining
Correct syntax for appending JSON data to a file
Which option correctly appends a JSON string to 'output.json' without overwriting existing content?
Attempts:
2 left
💡 Hint
Appending JSON data as a string with a newline keeps entries separated.
✗ Incorrect
Option B appends the JSON string plus a newline in one call, preserving existing content and formatting. Option B overwrites. Option B appends JSON and newline separately, which works but is less clean. Option B is asynchronous but lacks error handling and newline.