# Finding sequential digit numbers within a range in JavaScript

JavascriptWeb DevelopmentFront End Technology

## 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 −

Live Demo

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
]