
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding nearest Gapful number in JavaScript
A number is a gapful number when −
- It has at least three digits, and
- It is exactly divisible by the number formed by putting its first and last digits together
For example −
The number 1053 is a gapful number because it has 4 digits and it is exactly divisible by 13. Similarly, 135 is a gapful number because it has 3 digits and it is exactly divisible by 15.
Our job is to write a program that returns the nearest gapful number to the number we provide as input.
For example, for all 2-digit numbers, it would be 100. For 103, it would be 105.
We will break the problem into two functions −
isGapful() function
It receives a number string and returns a boolean as in the below code −
const isGapful = (numStr) => { const int = parseInt(numStr); return int % parseInt(numStr[0] + numStr[numStr.length - 1]) === 0; };
nearestGapful() function
This is our main function that receives a number, returns the nearest gapful number. Here’s the code −
const nearestGapful = (num) => { if(typeof num !== 'number'){ return -1; } if(num <= 100){ return 100; } let prev = num - 1, next = num + 1; while(!isGapful(String(prev)) && !isGapful(String(next))){ prev--; next++; }; return isGapful(String(prev)) ? prev : next; };
The isGapful() function returns a boolean based on whether the number is gapful or not in constant time, the nearestGapful() function loops until it finds a number that is gapful and returns it.
Following is the complete code −
Example
const n = 134; //receives a number string and returns a boolean const isGapful = (numStr) => { const int = parseInt(numStr); return int % parseInt(numStr[0] + numStr[numStr.length - 1]) === 0; }; //main function -- receives a number, returns a number const nearestGapful = (num) => { if(typeof num !== 'number'){ return -1; } if(num <= 100){ return 100; } let prev = num - 1, next = num + 1; while(!isGapful(String(prev)) && !isGapful(String(next))){ prev--; next++; }; return isGapful(String(prev)) ? prev : next; }; console.log(nearestGapful(n));
Output
The output in the console will be −
135
- Related Articles
- Finding Gapful number in JavaScript
- Finding nearest prime to a specified number in JavaScript
- Finding points nearest to origin in JavaScript
- JavaScript: Finding nearest prime number greater or equal to sum of digits - JavaScript
- Summing up digits and finding nearest prime in JavaScript
- Checking for the Gapful numbers in JavaScript
- Nearest Prime to a number - JavaScript
- Finding persistence of number in JavaScript
- Nearest power 2 of a number - JavaScript
- Finding whether a number is triangular number in JavaScript
- Finding number plate based on registration number in JavaScript
- Finding smallest number using recursion in JavaScript
- Finding unlike number in an array - JavaScript
- Finding closed loops in a number - JavaScript
- Finding product of Number digits in JavaScript
