-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.js
23 lines (18 loc) · 888 Bytes
/
Day02.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Problem 2: File Writer
// Problem Statement: Create a function writeToFile(filePath, content) that takes the path to a file and user input content as input. The function should write the content to the specified file using the fs module.
const fs = require('fs');
const path = require('path');
function writeToFile(filePath, content) {
fs.writeFile(filePath, content, 'utf8', (err) => {
if (err) {
console.error(`Error writing to file: ${err.message}`);
} else {
console.log(`Data written to ${path.basename(filePath)}`);
}
});
}
// Test Cases
writeToFile('test-files/output1.txt', 'Sample content.');
// Expected Output: Data written to output1.txt
writeToFile('test-files/nonexistent-folder/output.txt', 'Content in a non-existent folder.');
// Expected Output: Error writing to file: ENOENT: no such file or directory...