How to check if a number is pandigital in Node.js

1 Answer

0 votes
// A pandigital number is an integer that contains each digit from 0 to 9 
// at least once, with the condition that the leading digit must be nonzero

// for example, 1023456987 is a pandigital number 

function isPandigitalRange(num, start = 1, end = 9) {
    const str = String(num);

    // Build the expected digit string
    const expected = Array.from({ length: end - start + 1 }, (_, i) => String(i + start)).join('');

    // Sort digits of the number
    const digits = str.split('').sort().join('');

    return digits === expected;
}

console.log(isPandigitalRange(123456789));        // true
console.log(isPandigitalRange(1023456789, 0, 9)); // true
console.log(isPandigitalRange(123455789));        // false



/*
run:

true
true
false

*/

 



answered Dec 31, 2023 by avibootz
edited Feb 25 by avibootz
...