Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program to Find the Factors of a Number

JavaScript Program to Find the Factors of a Number

We have given the input number and we need to find all the factors of that number. Factors of the number are nothing but the numbers that divide the original input number evenly with the remainder of 0. Below we have added the example for better understanding:

Example:

Input: n = 10
Output: 1 2 5 10
Input: n = 12
Output: 1 2 3 4 6 12

Approach 1: Using For Loop

Here, we are finding the factors of the input number by using the simple and traditional method that is for loop. We are firstly iterating through the values of 1 to the input number (12). Then using the if condition, we are checking whether the n is exactly divisible or not. This is the simplest method to find the factors of the input number.

Example: This example implements the above-approach.

Javascript




let n = 12;
let i = 1;
for (i = 1; i < n; i++) {
    if (n % i == 0) {
        console.log(i);
    }
}
console.log(n);


Output

1
2
3
4
6
12

Approach 2: Using Spread Operator

Here we are using the Spread operator of JavaScript and the keys method to find the factors of the input number given by the user. The factors are stored in the array and printed using the console.log function.

Example: This example implements the above-approach.

Javascript




let fact = (n) =>
    [...Array(n + 1).keys()]
    .filter(
        (i) => n % i === 0
    );
console.log(fact(12));


Output

[ 1, 2, 3, 4, 6, 12 ]

Approach 3: Using the reduce method

Here, we will be using the reduce method to iterate over the elements and find the factors of the input number specific to the variable. This uses the ES6 feature for iteration and stores the output factors in the array. Then we print these factors in the console.

Example: This example implements the above-approach.

Javascript




let n = 12;
[...Array(n + 1).keys()].reduce(
    (_, i) => {
        if (i !== 0 && n % i === 0) {
            console.log(i);
        }
    }
);


Output

1
2
3
4
6
12

Approach 4: Using the map method

Here, we are using the map method which creates an array by calling the specific function on each of the elements present in the parent array. Mostly, the map method is used to iterate through the array and call function on every element of the array.

Example: This example implements the above-approach.

Javascript




let n = 12;
let fact = [...Array(n + 1).keys()]
    .map((i) => {
        if (n % i === 0) {
            return i;
        }
    })
    .filter((i) => i !== undefined);
console.log(fact);


Output

[ 1, 2, 3, 4, 6, 12 ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments