-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeNumberGenerator.js
More file actions
50 lines (33 loc) · 1.2 KB
/
PrimeNumberGenerator.js
File metadata and controls
50 lines (33 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//function that check whether a number is prime or not
function isItPrime(number) {
//if the number is a negative or 0, it is not a prime number
if (number <= 1) {
return false;
}
else {
/*checking whether number is prime. A number is only prime if it is divisible only by itself and 1.
Start checking for divisibility from 2 until the number before the number in question*/
for (let index = 2; index < number; index++) {
if (number % index === 0) {
return false;
}
}
return true;
}
}
//function that takes an array as input and returns a new array containing only prime numbers from original array.
function PrimeNumberGenerator(arr) {
//declare variable to store the array of prime numbers
let primeNumberArray = [];
//iterate through each number in the input array checking whether it is prime or not
for (let index = 0; index < arr.length; index++) {
//if the number is a prime number, push it to the array of prime numbers
if (isItPrime(arr[index])) {
primeNumberArray.push(arr[index]);
}
}
//output
return primeNumberArray;
}
//test function
console.log(PrimeNumberGenerator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));