Finding the count of numbers divisible by a number within a range using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a range of two integers as the first argument and a number as the second argument.

Our function should find all the numbers divisible by the input number in the specified range and return their count.

Example

Following is the code −

 Live Demo

const range = [6, 57];
const num = 3;
const findDivisibleCount = (num = 1, [l, h]) => {
   let count = 0;
   for(let i = l; i <= h; i++){
      if(i % num === 0){
         count++;
      };
   };
   return count;
};
console.log(findDivisibleCount(num, range));

Output

18
raja
Published on 20-Apr-2021 13:15:12
Advertisements