
- 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 sequential digit numbers within a range in JavaScript
Sequential Digits Number
A number has sequential digits if and only if each digit in the number is one more than the previous digit.
Problem
We are required to write a JavaScript function that takes in an array, arr, of exactly two elements specifying a range.
Our function should return a sorted array of all the integers in the range arr (limits inclusive) that have sequential digits.
For example, if the input to the function is −
const arr = [1000, 13000];
Then the output should be −
const output = [1234, 2345, 3456, 4567, 5678, 6789, 12345];
Example
The code for this will be −
const arr = [1000, 13000]; const sequentialDigits = ([low, high] = [1, 1]) => { const findCount = (num) => { let count = 0; while(num > 0){ count += 1 num = Math.floor(num / 10) }; return count; }; const helper = (count, start) => { let res = start; while(count > 1 && start < 9){ res = res * 10 + start + 1; start += 1; count -= 1; }; if(count > 1){ return 0; }; return res; }; const count1 = findCount(low); const count2 = findCount(high); const res = []; for(let i = count1; i <= count2; i++){ for(let start = 1; start <= 8; start++){ const num = helper(i, start); if(num >= low && num <= high){ res.push(num); }; }; }; return res; }; console.log(sequentialDigits(arr));
Output
And the output in the console will be −
[ 1234, 2345, 3456, 4567, 5678, 6789, 12345 ]
- Related Articles
- Finding sum of all numbers within a range in JavaScript
- Prime numbers within a range in JavaScript
- Finding the count of numbers divisible by a number within a range using JavaScript
- Finding Armstrong numbers in a given range in JavaScript
- Summing cubes of natural numbers within a range in JavaScript
- Finding nth digit of natural numbers sequence in JavaScript
- Finding the nth digit of natural numbers JavaScript
- Finding the largest 5 digit number within the input number using JavaScript
- Finding the least common multiple of a range of numbers in JavaScript?
- Armstrong number within a range in JavaScript
- Counting prime numbers that reduce to 1 within a range using JavaScript
- Finding the k-prime numbers with a specific distance in a range in JavaScript
- Finding the count of total upside down numbers in a range using JavaScript
- Generating desired pairs within a range using JavaScript
- Finding confusing number within an array in JavaScript

Advertisements