Repair division by zero
Difficulty: Easy
Try your first run
No account is needed to try this exercise. Write your answer, run the tests, and use the feedback to improve it.
- Read the examples and the required return value.
- Edit the supplied function in the editor, keeping its name and parameters. Return your answer, then select Run code.
- Use the test feedback to try again. Hints are available without replacing your code.
Repair the buggy starter to meet this contract: Return a/b for finite numbers, or null/None when b is zero.
Test-design prompt
Add 0/0 and explain why checking the numerator does not prevent failure.
Before editing, identify a minimal failing input and predict the result. After repairing, rerun the regression cases and explain why the old implementation failed.
Examples
1, 0null0, 206, -2-3Constraints
Inputs are JSON-compatible. Unless a tighter bound is stated, lists have at most 200 elements, strings at most 1000 characters, and integers are within -1000000..1000000. Do not mutate inputs. Use only the browser runner; no file or network access. Preserve the required function signature.
Review lesson: Read errors before changing codeA little nudge
Reveal one hint at a time. Give yourself a moment to try it.
- Run the smallest boundary or duplicate case and compare it with the exact contract.
- Guard the denominator. Python raises on zero division while JavaScript yields Infinity or NaN; neither matches the contract.
- Write an assertion for the original failure, repair the operation, and rerun the complete case set.
Worked solution
Try your own approach first, then compare the reasoning.
function solve(a,b){return b===0?null:a/b;}
Guard the denominator. Python raises on zero division while JavaScript yields Infinity or NaN; neither matches the contract. Regression design: Add 0/0 and explain why checking the numerator does not prevent failure.
Complexity
O(n log n) time for sorting tasks; otherwise O(n) time and output/storage space for n input elements, or O(1) for scalar checks. Nested-row transformations use n as the total number of cells.
Ready when you are
Write your solution, then run it against the practice tests.
Press Esc, then Tab to leave the editor. Code runs in an isolated browser workspace. Tests support learning, not verified grading.