0
0
Node.jsframework~20 mins

Watching files for changes in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Watcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you use fs.watch on a file?
Consider this Node.js code snippet using fs.watch to watch a file for changes. What will the output be when the file is modified?
Node.js
import { watch } from 'fs';

watch('example.txt', (eventType, filename) => {
  if (filename) {
    console.log(`${filename} file Changed: ${eventType}`);
  }
});
ALogs 'example.txt file Changed: change' each time the file is modified.
BLogs 'example.txt file Changed: rename' when the file content changes.
CThrows an error because fs.watch cannot watch files, only directories.
DLogs nothing because the callback is never called on file changes.
Attempts:
2 left
💡 Hint
Think about what eventType values fs.watch emits on file changes.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this fs.watch usage
Which option contains the correct syntax to watch a file named 'log.txt' and log changes?
Node.js
import { watch } from 'fs';

watch('log.txt', (event, file) => {
  console.log(`${file} was ${event}`);
});
Awatch('log.txt', (event, file) => { console.log(`${file} was ${event}`) };
Bwatch('log.txt', (event, file) => { console.log(`${file} was ${event}`) });
Cwatch('log.txt', (event, file) => console.log(`${file} was ${event}`));
Dwatch('log.txt', (event, file) => { console.log(`${file} was ${event}`); });
Attempts:
2 left
💡 Hint
Check for missing semicolons and braces in arrow functions.
🔧 Debug
advanced
2:00remaining
Why does this fs.watch code miss some file changes?
This code watches 'data.json' but sometimes misses changes. Why?
Node.js
import { watch } from 'fs';

watch('data.json', (eventType) => {
  if (eventType === 'change') {
    console.log('File changed');
  }
});
ABecause fs.watch is not 100% reliable and may miss rapid changes.
BBecause the callback does not check the filename parameter.
CBecause the watch function requires a third argument for recursive watching.
DBecause the eventType should be compared to 'rename' instead of 'change'.
Attempts:
2 left
💡 Hint
Consider the limitations of fs.watch on different platforms.
state_output
advanced
2:00remaining
What is the output count of this watcher after multiple changes?
Given this code watching 'notes.txt', if the file is changed 3 times quickly, how many times will the console log 'notes.txt changed'?
Node.js
import { watch } from 'fs';

let count = 0;
watch('notes.txt', (eventType, filename) => {
  if (eventType === 'change') {
    count++;
    console.log(`${filename} changed`);
  }
});
AExactly 3 times, once per change.
BAt least 1 time, but possibly fewer than 3 due to event coalescing.
C0 times, because the callback is not triggered for rapid changes.
DMore than 3 times, because each change triggers multiple events.
Attempts:
2 left
💡 Hint
Think about how fs.watch batches or coalesces events on some systems.
🧠 Conceptual
expert
2:00remaining
Which statement about fs.watch and fs.watchFile is true?
Choose the correct statement about the differences between fs.watch and fs.watchFile in Node.js.
ABoth <code>fs.watch</code> and <code>fs.watchFile</code> use the same underlying mechanism and have identical performance.
B<code>fs.watchFile</code> uses OS-level events and is more efficient than <code>fs.watch</code>.
C<code>fs.watch</code> uses OS-level events and is more efficient but less consistent across platforms than <code>fs.watchFile</code>.
D<code>fs.watch</code> polls the file system periodically, while <code>fs.watchFile</code> uses native OS events.
Attempts:
2 left
💡 Hint
Consider how each function detects changes under the hood.