Prime Number Finder

let array=[] let c=0 function primeFinder(n) { for (let i=2;i<=n;i++){ c=0 for (let a=2;a<i;a++){ let num=i/a if (num%1===0){ c++; } } if (c===0){ array.push(i) } } return array } console.log(primeFinder(11)) // Leave this so we can test your code: module.exports = primeFinder;

This is my code for the JavaScript challenge “Prime Number Finder” the code works well but I can’t pass the tests. Can anyone tell me if there is a problem with my code?

Hi,
you have defined the array outside of the function scope. So the return statement doesn’t return anything from within the function.
I assume that if you move the initialization of c and the array into the function body like this:

function primeFinder(n) {
  let array=[]
  for (let i=2;i<=n;i++){
    let c=0
...

it will probably pass the test.

Oh, thanks a lot! It works now.

1 Like