메모 삭제
Coddy Node.js 여정의 Node.js 기초 섹션에 포함된 레슨. 24개 중 12번째.
done 2는 메모 2를 삭제해야 하며, 사람들은 done 뒤에 아무 내용이나 입력합니다.
인수는 문자열입니다. Number('2')는 2이고 Number('two')는 NaN입니다. Number.isInteger는 한 번의 검사로 NaN과 2.5를 거부합니다. 그런 다음 범위를 확인합니다. 목록은 1부터 세고 배열은 0부터 세므로 메모 n은 인덱스 n - 1에 있으며, splice(i, 1)은 이를 삭제합니다.
// 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)}`);챌린지
쉬움done <n>은 메모 n을 제거하고 저장한 뒤Done: <text>를 출력합니다.- 메모가 아닌 단어(
abc,0,2.5, 마지막 메모 번호보다 큰 숫자)를 입력하면 stderr에No note <word>를 출력하고1로 종료되며, 아무것도 변경되지 않습니다.
done 1을 실행한 다음 list를 실행하세요. 그런 다음 done abc를 시도하세요.
직접 해보기
// 메모 도구는 여기서 시작합니다.
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);
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.