Explorer
Node.js

Write a function `divide(a, b, callback)` that divides a by b and calls callback in Node.js error-first style: `callback(error, result)`. If b is 0, pass an Error.

Problem Statement

Write a function `divide(a, b, callback)` that divides a by b and calls callback in Node.js error-first style: `callback(error, result)`. If b is 0, pass an Error.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Error-first callbacks have the signature: callback(err, result). šŸ’” Hint 2: If no error, pass null as the first argument. šŸ’” Hint 3: if (b === 0) callback(new Error("Division by zero")); else callback(null, a / b); āœ… Solution: ```javascript function divide(a, b, callback) { if (b === 0) { callback(new Error("Division by zero")); } else { callback(null, a / b); } } ```

Error-First Callback Pattern

Easy
Write a function `divide(a, b, callback)` that divides a by b and calls callback in Node.js error-first style: `callback(error, result)`. If b is 0, pass an Error.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[{"err":null,"res":5},{"err":"Division by zero"}]