1

I'm wanting to remove the non-prime numbers from an Array, the following is only removing the even numbers instead of the prime numbers.

function sumPrimes(num) {
  //Produce an array containing all number of to and including num
  let numArray = [];
  for (let i = 1; i <= num; i++) {
    numArray.push(i);
  }

  //Remove non-prime numbers from the array     
  numArray.map((number) => {
    for (let i = 2; i < number; i++) {
        if(number % i === 0) {
            let index = numArray.indexOf(number);
            return numArray.splice(index, 1);       
        }
    }   
  });

 return numArray;

}

sumPrimes(10);

This is currently returning:

[1, 2, 3, 5, 7, 9]

However, prime numbers are 1, 2, 3, 5, 7 (not include 9);

Nims
  • 195
  • 2
  • 2
  • 11
  • The output for me is 4, 6, 8, 9, 10, which is correct. You have programmed it to print when it finds a divisible number, not a non-divisible one. – Akshat Mahajan Jun 14 '17 at 00:42
  • `map` doesn't remove anything. Did you mean `filter`? Do not use `splice` within a loop. – Bergi Jun 14 '17 at 00:43
  • Your callback will need to `return` something meaningful, not `undefined`. – Bergi Jun 14 '17 at 00:43

4 Answers4

6

Use filter() instead:

var numArray = [2, 3, 4, 5, 6, 7, 8, 9, 10]

numArray = numArray.filter((number) => {
  for (var i = 2; i <= Math.sqrt(number); i++) {
    if (number % i === 0) return false;
  }
  return true;
});

console.log(numArray);
  • Thanks, I should have been using filter instead of map. Btw I do not need to use Math.sqrt(number); – Nims Jun 14 '17 at 01:05
  • Ok, but it's a lot faster for bigger numbers. There's not much point in checking if 97 is a factor of 100. –  Jun 14 '17 at 07:46
3

The previous answer doesn't work correctly for negative numbers.

As you can see here, the simplest way to find prime numbers is:

const array = [-5, -3, -2, -1, ...Array(20).keys()]; 
// Array(20).keys() generates numbers from 0 to 19.

function isPrime(num) {
  for (let start = 2; num > start; start++) {
    if (num % start == 0) {
      return false;
    }
  }
  return num > 1;
}

console.log(array.filter(isPrime)); // [2, 3, 5, 7, 11, 13, 17, 19]
1

The answer by Farnaz Kakhsaz works great, but it is slow. You only need to check until the root of 'num'. Thanks!

const array = [-5, -3, -2, -1, ...Array(20000).keys()]; 

function isPrime(num) {
  for (let i = 2; i <= Math.sqrt(num); i++) {
    if (num % i === 0) {
      return false;
    }
  }
  return num > 1;
}

console.log(array.filter(isPrime));
0

My solution for find prime numbers in an array in javascript using forEach().

let num = [-1,-2,-3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
let result = [];
function isPrime(num) {
  if(num < 2) return false;

  for (let k = 2; k < num; k++){
    if(num % k == 0){
      return false;
    }
  }
  return true;
}
num.forEach(function (element) {
  const item = isPrime(element);
  if (item) {
    result.push(element);
  }
});
console.log(result); // [ 2, 3, 5, 7, 11, 13, 17, 19]
Chetan Nada
  • 86
  • 1
  • 4