Hone logo
Hone
Problems

Find the Ceiling of a Number in JavaScript

Given a floating-point number, your task is to find its ceiling value. The ceiling of a number is the smallest integer that is greater than or equal to the number. This is a fundamental operation in programming, often used in scenarios like calculating resource allocation, rounding up for divisibility, or determining the next whole unit.

Problem Description

You will be provided with a single floating-point number as input. Your goal is to implement a JavaScript function that returns the ceiling of this number.

Requirements:

  • The function should accept one argument: a number (which can be an integer or a float).
  • The function should return an integer representing the ceiling of the input number.
  • You should use JavaScript's built-in mathematical functions if appropriate, but the core logic of finding the ceiling should be evident.

Expected Behavior:

  • If the input is an integer, the ceiling is the number itself.
  • If the input is a positive float, the ceiling is the next greater integer.
  • If the input is a negative float, the ceiling is the integer closer to zero (which is greater than the float).

Edge Cases:

  • Consider the input 0.
  • Consider negative floating-point numbers.

Examples

Example 1:

Input: 4.2
Output: 5
Explanation: The smallest integer greater than or equal to 4.2 is 5.

Example 2:

Input: -3.7
Output: -3
Explanation: The smallest integer greater than or equal to -3.7 is -3.

Example 3:

Input: 7
Output: 7
Explanation: Since 7 is an integer, its ceiling is itself.

Example 4:

Input: 0
Output: 0
Explanation: The smallest integer greater than or equal to 0 is 0.

Constraints

  • The input will always be a valid JavaScript number.
  • The input number will be within the standard JavaScript number range.
  • The solution should be efficient, with a time complexity that does not exceed O(1).

Notes

JavaScript provides a built-in Math.ceil() function. While using it directly would be the most idiomatic solution, consider the underlying logic if you were to implement it without Math.ceil() for a deeper understanding. Think about how integer division or modulo operations might play a role, or how to adjust a number based on its fractional part.

Loading editor...
javascript