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
divide(10, 4)returns 2.5- Dividing by 0 throws a RangeError
- Text instead of a number throws a TypeError
NaNis rejected too- Both errors have a message
- 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.