Computer >> Computer tutorials >  >> Programming >> Javascript

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 −

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