Command Palette

Search for a command to run...

Hectal
Phase 12: Modules and Tooling
JavaScript · Exercise 5 of 6 PreviousNext
Intermediate

Write npm's version check

Write a function satisfies(version, range) that returns true when the version is allowed by the range, the way npm decides it:

Exact, like 1.2.3: only that version. Tilde, like ~1.2.3: from 1.2.3 up to, not including, 1.3.0. Caret, like ^1.2.3: from 1.2.3 up to, not including, 2.0.0; for ^0.3.1 the limit is 0.4.0, and for ^0.0.3 it is 0.0.4 (the first non-zero number may not change).

Compare the parts as numbers: 1.10.0 is newer than 1.9.0.

What Check tests

  1. Exact versions match only themselves
  2. Caret allows minor and patch updates, not major
  3. Tilde allows only patch updates
  4. Caret below 1.0.0 is stricter
  5. Parts are compared as numbers, not text

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.