Menu
CoddyTech

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 icon

Challenge

Easy

When you finish a task, you mark it done and it leaves the list. Add a done command that removes a note by its number.

  1. In notes.js, add a done command. The word after done is the note's number, as list shows it, so note 1 is the first note. The word arrives as a string.
  2. Remove that note from the store, then save the notes to notes.json, the same way add saves them. Note 1 sits at index 0 of the array. You can put the removing code in notes.js or as a method in store.js.
  3. Print Done: followed by the text of the note you removed. With the notes Buy milk, Call Ada and Water the plants, node notes.js done 1 prints:
    Done: Buy milk
    notes.json then holds only Call Ada and Water the plants. done 3 removes Water the plants.
  4. If the word is not the number of a note, change nothing. Print No note followed by the word to stderr, and exit with code 1. That covers a word like abc, 0, a decimal like 2.5, and a number past the end, like 9 when there are three notes. node notes.js done abc prints 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);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Node.js Basics

Practice on your own: Express playground