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


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

Updated on: 20-Apr-2021

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements