Finding pandigital numbers using JavaScript


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.

A pandigital number is a number that contains all digits (0-9) at least once.

Therefore, let’s write the code for this function −

Example

The code for this will be −

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

The output in the console will be −

false
true

Updated on: 19-Oct-2020

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements