Return the nearest greater integer of the decimal number it is being called on in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that lives in the Math class of JavaScript.

Our function should return the nearest greater integer of the decimal number it is being called on.

If the number is already an integer, we should return it as it is.

Example

Following is the code −

 Live Demo

const num = 234.56;
Math.ceil = function(num){
   if(typeof num !== 'number'){
      return NaN;
   };
   if(num % 1 === 0){
      return num;
   };
   const [main] = String(num).split('.');  
   return +main + 1;
};
console.log(Math.ceil(num));

Output

Following is the console output −

235
raja
Published on 20-Apr-2021 08:00:57
Advertisements