Command Palette

Search for a command to run...

Hectal
Phase 13: Errors, Debugging and Testing
JavaScript · Exercise 1 of 5Next
Beginner

A divide function that refuses bad input

divide(a, b) returns a / b. Right now divide(5, 0) quietly returns Infinity and divide("6", 2) returns 3, a silent conversion from text.

Add guard clauses at the top: throw a TypeError if a or b isn't a finite number (Number.isFinite), and a RangeError if b is 0. Give each error a helpful message.

What Check tests

  1. divide(10, 4) returns 2.5
  2. Dividing by 0 throws a RangeError
  3. Text instead of a number throws a TypeError
  4. NaN is rejected too
  5. Both errors have a message
  6. No uncaught errors

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.