Ceil Number in JavaScript
The Math.ceil() function in JavaScript rounds a number up to the nearest integer. This is a fundamental mathematical operation with applications in various fields, such as financial calculations, resource allocation, and data analysis where you need to ensure you have enough to cover a requirement. This challenge asks you to implement a function that replicates the behavior of Math.ceil().
Problem Description
You are tasked with creating a JavaScript function called ceilNumber that takes a single numerical argument and returns the smallest integer greater than or equal to that number. The function should accurately round up any given number, whether it's positive, negative, or zero, and whether it's an integer or a floating-point number.
Key Requirements:
- The function must accept a single numerical argument.
- The function must return an integer.
- The returned integer must be the smallest integer greater than or equal to the input number.
- The function should handle both positive and negative numbers correctly.
- The function should handle integer and floating-point numbers correctly.
Expected Behavior:
ceilNumber(3.2)should return4.ceilNumber(3.0)should return3.ceilNumber(-3.2)should return-3.ceilNumber(-3.0)should return-3.ceilNumber(0)should return0.
Edge Cases to Consider:
- Very large or very small numbers (consider potential precision issues, though this is less critical for this problem).
- Non-numeric input (the function should ideally handle this gracefully, though the prompt doesn't explicitly require error handling). For simplicity, assume the input will always be a number.
Examples
Example 1:
Input: 3.2
Output: 4
Explanation: 3.2 is between 3 and 4, and 4 is the smallest integer greater than or equal to 3.2.
Example 2:
Input: -3.8
Output: -3
Explanation: -3.8 is between -4 and -3, and -3 is the smallest integer greater than or equal to -3.8.
Example 3:
Input: 5
Output: 5
Explanation: 5 is already an integer, so the smallest integer greater than or equal to 5 is 5 itself.
Constraints
- The input will always be a number (integer or floating-point).
- The function must return an integer.
- The function should be reasonably efficient (avoid unnecessary computations).
Notes
Consider using basic arithmetic operations to achieve the desired rounding behavior. Think about how the fractional part of a number affects the result. You don't need to worry about handling non-numeric inputs for this challenge.