Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Safe Divide

Asynchronous JavaScripteasy

Course · Section 16: Asynchronous JavaScript: Promises, Async/Await, and AJAX · Lecture 275: Error Handling With try...catch

Implement `safeDiv(a, b)` that throws when `b` is `0`, catches it, and returns `null`; otherwise returns `a / b`.

Sample tests

Input: safeDiv(10, 2)
Output: 5
Input: safeDiv(5, 0)
Output: null

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Returning Infinity by dividing by zero without the guard.

Learning resources

  • MDN: try...catch
Approach & explanation (try first)

The try/catch converts the zero-divisor error into a null result.

Loading...
⌘/Ctrl + Enter

Run your code to see results.