JavaScript Challenge - Prime Number Finder

Hi All,

Here is my solution to this challenge:

``````const primeFinder = number => {
let outputArray = [];
for (let i = 1; i <= number; i++) {
let factorCounter = 0;
for (let n = 1; n <= i; n++) {
if (i % n == 0) {
factorCounter++;
}

}
if (factorCounter == 2) {
outputArray.push(i);
}
}
return outputArray;
}

console.log(primeFinder(12))

// Leave this so we can test your code:
module.exports = primeFinder;
``````
``````function primeFinder(n) {
let prime = [];
for(let a = 2; a <= n; a++){
prime.push(a);
for(let b = 2; b < a; b++){
if(a%b == 0){
prime.pop();
break;
}
}
}
return prime
}

console.log(primeFinder(11))

// Leave this so we can test your code:
module.exports = primeFinder;
``````
function primeFinder(n) { let i, j; let ArrayFinal = []; let primeArray = Array.from({length: n + 1}, (_, i) => true); primeArray[0]=false; primeArray[1]=false; for(i = 2 ; i<= Math.sqrt(n); i++){ if(primeArray[i] == true){ for( j = i * i; j <= n; j += i){ primeArray[j] = false; } } } primeArray.forEach((p, index) => p == true && ArrayFinal.push(index)); return ArrayFinal; } console.log(primeFinder(11)); // Leave this so we can test your code: module.exports = primeFinder;
``````const _ = require('lodash');

// Return true if n is prime
function primeFinder(n) {
function isPrime(n)
{
for (let i = 2; i**2 <= n; i++) {
if (!(n % i)) return false
}
return n > 1;
}
return _.range(1,n + 1).filter(isPrime)
}

console.log(primeFinder(11))

// Leave this so we can test your code:
module.exports = primeFinder;
``````

can change

by

``````function isItPrime(n) {
for (let i = 2; i**2 < n; i++) {
if (!(n % i) ) {
return false;
}
}
return n > 1;
}``````

const primeFinder = num => {
if (num <= 1) {
return
}

``````let prime = true
let number = 2
while( number <= num){
prime = true
for (let y = 0; y <= (primeNumbers.length - 1) - 1; y++) {
if (number % primeNumbers[y] === 0) {
prime = false
break
}
}
}
if (prime) {
}

number = number > 2 ? number + 2 : number + 1
}

``````

}

console.log(primeFinder(11))

// Leave this so we can test your code:
module.exports = primeFinder;

``````function primeFinder(n) {
let arr = [];
let final =[];

function isPrime(num){
if (num <= 1) {
return false;
} else {
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
}

for(let i=2; i<=n; i++){
if(isPrime(i)){
arr.push(i)
}

}
return arr
}

// Leave this so we can test your code:
module.exports = primeFinder;

``````
``````function primeFinder(n) {
let primeList = [];

for (let i = 2; i <= n; i++) {
let isPrime = true;

for (let j = 2; j < i; j++) {
if (i % j === 0) {
isPrime = false;
}
};

if (isPrime === true) {
primeList.push(i);
};
}

return primeList;
}

console.log(primeFinder(11))

// Leave this so we can test your code:
module.exports = primeFinder;
``````
function isPrime(n, primes) { if (n == 2) { return true; } for(let i = 0; i <= primes.length; i++) { if (0 === n % primes[i]) return false; if (primes[i] * primes[i] > n) { break; } } return true; } function primeFinder(n) { let primes = []; for (let i = 2; i <= n; i++) { if (isPrime(i, primes)) primes.push(i); } return primes; } console.log(primeFinder(13)) // Leave this so we can test your code: module.exports = primeFinder;
``````function primeFinder(n) {
const results = [];
for (let i = 2; i <= n; i++) {
if (isPrime(i)) {
results.push(i);
}
}
return results;
}

function isPrime(num) {
for (let i = 2; i <= num/2; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
``````
function primeFinder(n) { let primes = [] for (let i=2; i<= n ; i++){ let count = 0 for (let j=2; j<=i; j++) { if (i%j===0) { count++ } } count>1 ? count=0 : primes.push(i) } return primes } console.log(primeFinder(11)) // Leave this so we can test your code: module.exports = primeFinder;

function primeFinder(n) {
let output = [2];
let number = 3;
while(number <= n){
var flag = true;
for(var i=2; i<number-1;i++){

`````` if(number%i == 0){
flag = false;
break;
}

}
if(flag){
output.push(number);
}
number++;
``````

}
return output;
}

console.log(primeFinder(11))

// Leave this so we can test your code:
module.exports = primeFinder;

Here’s my code!

const isPrime = (num) => { for (let i = 2, s = Math.sqrt(num); i <= s; i++) if (num % i === 0) return false; return num > 1; }; function primeFinder(n) { let primeNums = []; for (let i = 0; i <= n; i++) { isPrime(i) ? primeNums.push(i) : null; } if(primeNums.length > 1){ return primeNums }else{ return null } } console.log(primeFinder(11)) // Leave this so we can test your code: module.exports = primeFinder;
function primeFinder(n) { let primes = [] for(let i = 2; i<=n; i++){ let count = 0; for(let j = 1; j<=i; j++){ if(i % j == 0) count++; } if(count == 2) primes.push(i); } return primes; } console.log(primeFinder(11)) // Leave this so we can test your code: module.exports = primeFinder;
``````function primeFinder(n) {
const list =[];
for(let i=2;i<=n;i++){
list.push(i);
}
return list.filter(no=>{
let [factors,j]=[[],1];
while(j<=no){
if(no%j===0){
factors.push(j);
}
j++;
}
return factors.length===2;
})
}
``````
function checkPrime(n) { for (let i = 2; i < n; i++) { if((n%i) == 0) { return false; } } return true; } function primeFinder(n) { // Write your code here let primeArray = []; for(let i = 2; i <= n; i++) { if(checkPrime(i)) { primeArray.push(i); } } return primeArray; } console.log(primeFinder(11)) // Leave this so we can test your code: module.exports = primeFinder;

"Tests failed to run due to an error: "Looks like your code is not compiling or not being exported with `module.exports = primeFinder`. Check for a syntax error. “. Check your code and try again.”

If you see something let me know.
const isPrime = (num) => { switch (num) { case 1: return false case 2: return true default: return checkPrime(num) } } const checkPrime = (num) => { for (let i = 2 ; i < num; i++) { if (num % i === 0) { return false; } } return true } const isOdd = (num) => { return num % 2 != 0 ? true : false; }; const primeFinder = (n) => { const primes = []; for (i = 1; i <= n; i++) { //if (isOdd(i)) { if (isPrime(i)) primes.push(i); //} } console.log(primes); return primes; }; primeFinder(24);

If the code posted in your CodeByte was the same as in the unit then you are missing the following:

``````// Leave this so we can test your code:
module.exports = primeFinder;
``````

I did not include it. It is there on the challenge version. Thanks for the reply.

This code was confusing but cleared up as I stepped through with liberal amounts of console.log statements.

Below is your code with my comments for my understanding. JavaScript is not a problem for me. The why of the approach is. Is the initialized primes array used to gain a performance advantage? Why are there 12 elements and not 11?

I understand that square root optimization eliminates having to deal with repeated factors. I am fuzzy on the inner loop. Why x = 2*y.

In the mental logic paragraph, you make this statement: " the code could not solve by itself," What does this mean?

``````const primeFinder = function (n) {
//create an array [false, false] concat 10 true elements
//I know what is being done here, I do not understand why
const primes = [false, false].concat(new Array(n - 1).fill(true));

//I understand the square root optimization but not clear on this implementation
const r = Math.floor(Math.sqrt(n) + 1);
for (let y = 2; y < r; y++) {

console.log('outer - y: ', y, 'r: ', r)

for (let x = 2 * y; x <= n; x += y) {

console.log('x: ', x, 'y: ', y );

primes[x] = false;

}
}
//console.log(primes);
// primes = [ false, false, true, true,  false, true, false, true,  false, false, false, true ]
// At this point, primes represent the numbers 0-11, the trues represent the primes
//Iterate thru primes and set the trues to their corresponding prime
primes.forEach((x, i) => {
if (x) {
primes[i] = i;
}
})
//console.log(primes);
// primes = [ false, false, 2, 3,  false, 5, false, 7,  false, false, false, 11 ]
return primes.filter(x => !! x);  // not false is true, not not false is false, not not true is true
//primes = [2, 3, 5, 7, 11]
}
console.log(primeFinder(11))
``````