# freeCodeCamp Challenge Guide: Factors of an integer

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-factors-of-an-integer/302265>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 6:39pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-factors-of-an-integer/302265 "2019-08-03T18:39:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 6:39pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-factors-of-an-integer/302265/1 "2019-08-03T18:39:57Z")

</div>

# [Factors of an integer](https://www.freecodecamp.org/learn/coding-interview-prep/rosetta-code/factors-of-an-integer)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function factors(num) {
> const arr = [];
> for (let i = 2; i <= num/2; i++) {
> if (num % i === 0) {
> arr.push(i);
> }
> }
> return [1, ...arr, num]; 
> }
> 
> ```

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function factors(num) {
> return Array
> .from(Array(num + 1), (_, i) => i)
> .filter(i => num % i === 0)
> }
> 
> ```
> 
> This solution uses ES6. Using the `Array` class, create an array whose length is equal to `n + 1`. Use the Array.from method to replace every every array element with its index value. Since this is initially an empty array, we use `_` as the item parameter to show that this parameter is not needed when we replace the values.
> 
> Then, implement a filter using Array.filter. For each array element, we check if it divides `num` using `num % i` . If it does, `i` is a factor, and it stays in the array.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:49pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-factors-of-an-integer/302265/2 "2025-01-08T15:49:10Z")

</div>


