Przejdź do treści głównej
← ⚡ JavaScript & the browser·Moduł C8 · Lekcja 10
ZadanieCombine the runner from C8-05 (with describe) and assertEqual from C8-09. Implement createSortedSet() with .add, .has, .size, .toArray. Inside describe('sortedSet'), write three tests: 'starts empty', 'add then has', 'dedupes and sorts' (assertDeepEqual after adding 3,1,3,2 the toArray() is [1,2,3]). runAll() — all 3 pass.

Capstone: test a small library

275 XP15 min
Teoria

Putting C8 together

Combine every C8 idea to test a real micro-library — a sorted, dedup'd integer set. You'll write the library AND the tests for it in one file.

function createSortedSet() {
  const items = [];
  return {
    add(x)    { if (!items.includes(x)) items.push(x); items.sort((a,b) => a - b); },
    has(x)    { return items.includes(x); },
    size()    { return items.length; },
    toArray() { return items.slice(); },
  };
}

The capstone wires C8-05 runner + C8-09 better assertEqual, then writes a small suite that covers happy path, dedup, and ordering.

Why testing a library is the natural exercise

Libraries have clear inputs and outputs — perfect for tests. Plus you're going to write libraries throughout your career; learning to test them well is a permanent skill.

🔒

Zarejestruj się, aby zacząć kodować

Teoria jest dostępna dla każdego. Edytor, podgląd na żywo i check odblokowują się z 7-dniowym darmowym okresem próbnym — wymagana karta, anuluj w dowolnym momencie.

Zarejestruj się — darmowy okres próbny →

Pierwsze 10 lekcji w każdej ścieżce jest darmowych. Karta nie jest wymagana.

← PoprzedniaNastępna lekcja →

Codziennie jedna wskazówka Python lub web — na maila

Krótko, ręcznie pisane, bez spamu. Wypisanie w jedno kliknięcie.