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


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

Updated on: 20-Apr-2021

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements