Removing a Note
Part of the Node.js Basics section of Coddy's Node.js journey. Lesson 15 of 33.
done 2 should remove note 2, and people type anything after done.
Arguments are strings: Number('2') is 2 and Number('two') is NaN. Number.isInteger rejects NaN and 2.5 in one test; then check the range. Lists count from 1 and arrays from 0, so note n sits at index n - 1, and splice(i, 1) removes it.
// node seat.js 4
const seat = Number(process.argv[2]);
if (!Number.isInteger(seat) || seat < 1 || seat > 30) {
console.error(`No seat ${process.argv[2]}`);
process.exit(1);
}
console.log(`Row ${Math.ceil(seat / 6)}`);Challenge
EasyWhen you finish a task, you mark it done and it leaves the list. Add a done command that removes a note by its number.
- In
notes.js, add adonecommand. The word afterdoneis the note's number, aslistshows it, so note 1 is the first note. The word arrives as a string. - Remove that note from the store, then save the notes to
notes.json, the same wayaddsaves them. Note 1 sits at index 0 of the array. You can put the removing code innotes.jsor as a method instore.js. - Print
Done:followed by the text of the note you removed. With the notesBuy milk,Call AdaandWater the plants,node notes.js done 1prints:Done: Buy milknotes.jsonthen holds onlyCall AdaandWater the plants.done 3removesWater the plants. - If the word is not the number of a note, change nothing. Print
No notefollowed by the word to stderr, and exit with code 1. That covers a word likeabc,0, a decimal like2.5, and a number past the end, like9when there are three notes.node notes.js done abcprints this, in red:No note abc
Try it: type list in the box after $ node notes.js to see your notes and their numbers. Type done 1, then list again: the first note is gone and the others moved up one number. Then type done abc: you see No note abc in red and exit code 1, and list shows the same notes as before.
Try it yourself
// Your notes tool starts here.
import { rmSync } from 'node:fs';
import path from 'node:path';
import { formatNote } from './format.js';
import stats from './legacy/stats.cjs';
import store from './store.js';
const [command, ...words] = process.argv.slice(2);
const text = words.join(' ');
if (command === 'add') {
const n = store.add(text);
console.log(`Saved note ${n}.`);
} else if (command === 'list') {
const owner = process.env.NOTES_OWNER ?? 'you';
console.log(`Notes for ${owner}`);
if (store.notes.length === 0) {
console.log('No notes yet');
}
store.notes.forEach((note, i) => console.log(formatNote(i + 1, note)));
} else if (command === 'where') {
const file = path.join(process.cwd(), 'notes.json');
console.log(file);
console.log(path.basename(file));
} else if (command === 'stats') {
console.log(`${store.notes.length} notes, ${stats.countWords(store.notes)} words`);
} else if (command === 'clear') {
rmSync('notes.json', { force: true });
console.log('Deleted notes.json');
} else {
if (command) {
console.error(`Unknown command: ${command}`);
}
console.error('Usage: node notes.js <command> [words]');
process.exit(1);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Node.js Basics
1Running JavaScript on Node
Hello, NodeReading the CommandCommand-Line ArgumentsCommands3Files
JSON Text and ValuesReading a FileWriting JSON TextWriting a FileRemoving a NotePractice on your own: Express playground