Number of digits that divide the complete number in JavaScript

JavascriptWeb DevelopmentFront End Technology

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should count and return the number of digits present in the number that completely divide the number.

For example −

If the input number is −

const num = 148;

Then the output should be −

const output = 2;

because 148 is exactly divisible by 1 and 4 but not 8.

Example

The code for this will be −

 Live Demo

const num = 148;
const countDividingDigits = (num = 1) => {
   let count = 0;
   const numStr = String(num);
   for(let i = 0; i < numStr.length; i++){
      const curr = +numStr[i];
      if(num % curr === 0){
         count++;
      };
   };
   return count;
};
console.log(countDividingDigits(num));

Output

And the output in the console will be −

2
raja
Published on 24-Feb-2021 05:49:03
Advertisements