How to useSpread Operator in Javascript
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.
let fact = (n) =>
[...Array(n + 1).keys()]
.filter(
(i) => n % i === 0
);
console.log(fact(12));
Output
[ 1, 2, 3, 4, 6, 12 ]
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
Table of Content
- Using For Loop
- Using Spread Operator
- Using the reduce method
- Using the map method
Contact Us