JavaScript - Checking for pandigital numbers


A pandigital number is a number that contains all digits (0-9) at least once. We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.

Example

Following is the code to check for pandigital numbers −

const numStr1 = '47458892414';
const numStr2 = '53657687691428890';
const isPandigital = numStr => {
   let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
   for(let i = 0; i < numStr.length; i++){
      if(!legend.includes(numStr[i])){
         continue;
      };
      legend.splice(legend.indexOf(numStr[i]), 1);
   };
   return !legend.length;
};
console.log(isPandigital(numStr1));
console.log(isPandigital(numStr2));

Output

Following is the output in the console −

false
true

Updated on: 18-Sep-2020

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements