Putting it together
- Node.js course
- Beginner
- Lesson 116/116
- 6 min
Combine all parts into a working CLI app.
Putting it all together
The final lesson: we combine all parts into a working app. The addTask, completeTask, saveTasks and loadTasks functions are ready. Now we wire them into a flow.
The flow is: add tasks, complete one, save, then reset the array and reload from file. This proves persistence works: when we reload the saved data we should see the same tasks. Finally we count completed tasks with filter. tasks.filter(function(t) { return t.done; }).length gives the completed count. We take the previous lesson's solution and add this flow.
Counting with filter
At the end of the flow we count completed tasks with filter. tasks.filter(function(t) { return t.done; }) returns a new array with only tasks whose done property is true. We take its .length.
find returns the first match while filter returns all matches. This small difference does a big job: find gives one item, filter gives a subset. We assign this count to a doneCount variable and print it with console.log. This shows the app's final summary: total tasks and completed tasks.
Practice Combine all parts
Take the previous lesson's solution. Add this flow: reset with tasks = [];. Add two tasks with addTask("Meeting") and addTask("Report"). Complete the first with completeTask(1). Save with saveTasks(). Reset again with tasks = [];. Reload with loadTasks(). Print console.log("Total: " + tasks.length). Count with let doneCount = tasks.filter(function(t) { return t.done; }).length;. Print console.log("Done: " + doneCount).
Starter code
const path = require("path");
const fs = require("fs");
const DATA_FILE = path.join("data", "tasks.json");
function createTask(id, title) {
return { id: id, title: title, done: false };
}
let tasks = [];
tasks.push(createTask(1, "Shopping"));
function addTask(title) {
const id = tasks.length + 1;
tasks.push(createTask(id, title));
}
function completeTask(id) {
const task = tasks.find(function(t) { return t.id === id; });
if (task) { task.done = true; }
}
function saveTasks() {
fs.writeFileSync(DATA_FILE, JSON.stringify(tasks, null, 2));
}
function loadTasks() {
try {
const raw = fs.readFileSync(DATA_FILE, "utf8");
tasks = JSON.parse(raw);
} catch (e) {
tasks = [];
}
}
addTask("Email");
completeTask(1);
saveTasks();
console.log("Saved");
// Combine all parts: add a task, complete it, save, reload
// write hereSample solution
const path = require("path");
const fs = require("fs");
const DATA_FILE = path.join("data", "tasks.json");
function createTask(id, title) {
return { id: id, title: title, done: false };
}
let tasks = [];
tasks.push(createTask(1, "Shopping"));
function addTask(title) {
const id = tasks.length + 1;
tasks.push(createTask(id, title));
}
function completeTask(id) {
const task = tasks.find(function(t) { return t.id === id; });
if (task) { task.done = true; }
}
function saveTasks() {
fs.writeFileSync(DATA_FILE, JSON.stringify(tasks, null, 2));
}
function loadTasks() {
try {
const raw = fs.readFileSync(DATA_FILE, "utf8");
tasks = JSON.parse(raw);
} catch (e) {
tasks = [];
}
}
addTask("Email");
completeTask(1);
saveTasks();
console.log("Saved");
tasks = [];
addTask("Meeting");
addTask("Report");
completeTask(1);
saveTasks();
tasks = [];
loadTasks();
console.log("Total: " + tasks.length);
let doneCount = tasks.filter(function(t) { return t.done; }).length;
console.log("Done: " + doneCount);Expected output
Kaydedildi Toplam: 2 Tamamlanan: 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each flow step to its order
| Term | Meaning |
|---|---|
1. addTask |
Adds a task |
2. completeTask |
Marks a task done |
3. saveTasks |
Writes to file |
4. loadTasks |
Reads from file |
2 What does tasks.filter(function(t) { return t.done; }) return?
tasks.filter(function(t) { return t.done; })- An array of completed tasks Correct answer
- The number of tasks
- A single task
- The first task
Explanation: filter returns an array of all matches.
3 Complete the loadTasks call
tasks = [];
____();
console.log("Toplam: " + tasks.length);
tasks = [];
loadTasks();
console.log("Toplam: " + tasks.length);
Explanation: It loads saved tasks from the file.