Safe Division - Problem
Implement a safe division function that gracefully handles various error conditions without crashing the program.
Your function should:
- Handle division by zero - return
"Error: Division by zero" - Handle non-numeric inputs - return
"Error: Invalid input" - Perform normal division for valid numeric inputs
- Return results as strings for consistency
The function takes two parameters that could be numbers, strings, or other data types, and must validate them before attempting division.
Input & Output
Example 1 — Valid Division
$
Input:
dividend = "10", divisor = "2"
›
Output:
"5"
💡 Note:
Both inputs are valid numbers. 10 ÷ 2 = 5, returned as string "5"
Example 2 — Division by Zero
$
Input:
dividend = "10", divisor = "0"
›
Output:
"Error: Division by zero"
💡 Note:
Divisor is zero, which would cause a division by zero error, so return error message
Example 3 — Invalid Input
$
Input:
dividend = "abc", divisor = "2"
›
Output:
"Error: Invalid input"
💡 Note:
First parameter "abc" cannot be converted to a number, so return invalid input error
Constraints
- Input parameters can be strings, numbers, or other data types
- Must handle all non-numeric inputs gracefully
- Must prevent division by zero errors
- Return all results as strings for consistency
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code