Challenge: File System Operations
Swipe to show menu
Goal
Master the art of task management automation! Your mission is to develop an application that gathers tasks from one source, extracts their content, and integrates them into another file. Your solution should also handle any potential errors along the way.
Task
Imagine you have two files:
tasks.txt, which contains a list of existing tasks;new-task.txt, which contains a single task that should be added totasks.txt.
Follow these steps to complete the challenge:
-
Prepare your workspace
- Create a new folder on your machine;
- Open it in your preferred code editor.
-
Create
tasks.txt-
Create a file named
tasks.txt; -
Add the following tasks to it, or download the prepared file:
Contents:
Teach a goldfish to play chess;Build a sandcastle in your living room;Create a song 🎶 using only sounds from nature.
-
-
Create
new-task.txt-
Create a file named
new-task.txt; -
Add the following task to it, or download the prepared file:
Content:
Invent a new dance move and perform it in public.
-
-
Create
app.js- Import the
fsmodule to work with files; - Use
readFile()to read the content ofnew-task.txt; - Handle successful and failed operations using
.then()and.catch(); - Inside the
.then()block, useappendFile()to add the content totasks.txt; - Append a newline character (
\n) after the inserted task.
- Import the
-
Run the application
- Save
app.js; - Open the terminal and run:
node app - Save
If you prefer to use the code editor below, keep in mind that it does not recognize your files and will not show your progress.
123456789const fs = require("fs").___; fs.___("new-task.txt", "utf-8") .then(___ => { return fs.___("tasks.txt", ___ + ___); }) .___((error) => { console.log("Error:", error); });
- Import the
fsmodule to work with files. - Use the
fs.readFile("new-task.txt", "utf-8")to read content from the filenew-task.txt. - Chain a
.then()to handle the successful reading of the file content. - In the
.then()block, provide a callback function that receives the content as an argument. - Inside the
.then()block of thereadFilepromise, return the result of usingfs.appendFileto add content to the filetasks.txt. - Remember to include a newline character
\nto separate the appended content. - After the
.then()block of theappendFileoperation, chain a.catch()block to handle any errors that might occur in the entire chain. - In the
.catch()block, provide a callback function that receives an error parameter. - Inside the
.catch()block, log an error message along with the received error. - Run the script using Node.js by typing
node app.
const fs = require("fs").promises;
fs.readFile("new-task.txt", "utf-8")
.then((content) => {
return fs.appendFile("tasks.txt", content + "\n");
})
.catch((error) => {
console.log("Error occurred:", error);
});
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Challenge: File System Operations
Goal
Master the art of task management automation! Your mission is to develop an application that gathers tasks from one source, extracts their content, and integrates them into another file. Your solution should also handle any potential errors along the way.
Task
Imagine you have two files:
tasks.txt, which contains a list of existing tasks;new-task.txt, which contains a single task that should be added totasks.txt.
Follow these steps to complete the challenge:
-
Prepare your workspace
- Create a new folder on your machine;
- Open it in your preferred code editor.
-
Create
tasks.txt-
Create a file named
tasks.txt; -
Add the following tasks to it, or download the prepared file:
Contents:
Teach a goldfish to play chess;Build a sandcastle in your living room;Create a song 🎶 using only sounds from nature.
-
-
Create
new-task.txt-
Create a file named
new-task.txt; -
Add the following task to it, or download the prepared file:
Content:
Invent a new dance move and perform it in public.
-
-
Create
app.js- Import the
fsmodule to work with files; - Use
readFile()to read the content ofnew-task.txt; - Handle successful and failed operations using
.then()and.catch(); - Inside the
.then()block, useappendFile()to add the content totasks.txt; - Append a newline character (
\n) after the inserted task.
- Import the
-
Run the application
- Save
app.js; - Open the terminal and run:
node app - Save
If you prefer to use the code editor below, keep in mind that it does not recognize your files and will not show your progress.
123456789const fs = require("fs").___; fs.___("new-task.txt", "utf-8") .then(___ => { return fs.___("tasks.txt", ___ + ___); }) .___((error) => { console.log("Error:", error); });
- Import the
fsmodule to work with files. - Use the
fs.readFile("new-task.txt", "utf-8")to read content from the filenew-task.txt. - Chain a
.then()to handle the successful reading of the file content. - In the
.then()block, provide a callback function that receives the content as an argument. - Inside the
.then()block of thereadFilepromise, return the result of usingfs.appendFileto add content to the filetasks.txt. - Remember to include a newline character
\nto separate the appended content. - After the
.then()block of theappendFileoperation, chain a.catch()block to handle any errors that might occur in the entire chain. - In the
.catch()block, provide a callback function that receives an error parameter. - Inside the
.catch()block, log an error message along with the received error. - Run the script using Node.js by typing
node app.
const fs = require("fs").promises;
fs.readFile("new-task.txt", "utf-8")
.then((content) => {
return fs.appendFile("tasks.txt", content + "\n");
})
.catch((error) => {
console.log("Error occurred:", error);
});
Thanks for your feedback!