
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return Nearest Greater Integer of Decimal Number 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
Advertisements