# Prime numbers code logic

**URL:** <https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 17, 2021, 10:04am UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368 "2021-06-17T10:04:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![naimiii](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naimiii/32/125846_2.png) [@naimiii](https://forum.freecodecamp.org/u/naimiii)\
**Post date:** [June 17, 2021, 10:04am UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/1 "2021-06-17T10:04:06Z")

</div>

I have to change the logic of my prime numbers code. Right Now I don’t wanna use the filter and arrow method in my code. How can I do that?  
Here’s my code :let number = [1, 8, 7, 3, 2, 9, 10, 12];

number = number.filter((myArray) =\> {  
for (let i = 2; i \<= Math.sqrt(myArray); i++) {  
if (myArray % i === 0) {  
return false;  
}  
return true;  
}  
});  
console.log(number);

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [June 17, 2021, 11:26am UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/2 "2021-06-17T11:26:19Z")

</div>

First, your function doesn’t work correctly at the moment, because you are returning `true` within the for loop.

Second, if you don’t want to use `.filter`, why did you choose to use `.filter` in the first place? Unless you clarify, I’m assuming that this is some code snippet you copied from elsewhere, but you’re not comfortable with the methods and syntax and now want to change it to something simpler.

Remember that `.filter` is an array method that technically does the same as a for loop:

- it loops over the array
- pushes all the numbers that pass the “prime” test into a new array
- finally returns that new array

With that list given, you should be able to rewrite the code so that it uses a for loop instead of `.filter` (you’ll have two for loops then, an outer an an inner loop).

---

<div class="post-metadata">

**Author:** ![naimiii](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naimiii/32/125846_2.png) [@naimiii](https://forum.freecodecamp.org/u/naimiii)\
**Post date:** [June 17, 2021, 12:27pm UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/3 "2021-06-17T12:27:06Z")

</div>

@jsdisco thanks for the detailed answer but I only wanna know that substitute of arrow function.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [June 17, 2021, 12:58pm UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/4 "2021-06-17T12:58:28Z")

</div>

The variable you’ve called `myArray` is a number, not an array, that’s super confusing. But anyway

```auto
(someValue) => {
  return // do some stuff
}

```

```auto
function (someValue) {
  return // do some stuff
}

```

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [June 17, 2021, 2:58pm UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/5 "2021-06-17T14:58:50Z")

</div>

> [@DanCouper](#):
>
> The variable you’ve called `myArray` is a number, not an array, that’s super confusing. But anyway

Yeah, plus the variable `number` is actually an array 😅

Sorry @naimiii not trying to make fun of you. @DanCouper already showed the pattern for how to replace an arrow function with a regular function, but get back if you’re still struggling to apply that to your code.

---

<div class="post-metadata">

**Author:** ![naimiii](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naimiii/32/125846_2.png) [@naimiii](https://forum.freecodecamp.org/u/naimiii)\
**Post date:** [June 18, 2021, 12:14pm UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/6 "2021-06-18T12:14:52Z")

</div>

@jsdisco I have made the logic by following @DanCouper comment.  
here’s my logic using regular function

```auto
function checkPrime(number) {
  if (number <= 1) {
    return false;
  } else {
    for (let i = 2; i < number; i++) {
      if (number % i == 0) {
        return false;
      }
    }
    return true;
  }
}
console.log(checkPrime(10));

```

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [June 18, 2021, 1:02pm UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/7 "2021-06-18T13:02:20Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [June 18, 2021, 1:06pm UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/8 "2021-06-18T13:06:47Z")

</div>

You only need to go up to the square root of `number` (so `i <= Math.sqrt(number);`) in the loop.

You can also return false before the loop if the number is an even number that isn’t 2. Then you can start the loop at 3 and increment by 2

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [December 18, 2021, 1:07am UTC](https://forum.freecodecamp.org/t/prime-numbers-code-logic/465368/9 "2021-12-18T01:07:31Z")

</div>

Closed on December 18, 2021, 1:07am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
