Check prime in Array

function checkPrime(num){
   for (let i = 2; i < num; i++){
     if(num % i === 0){
       }
     }
  }
  function detectPrime(arr){
      if (checkPrime){
          return true;
           }else{
             return false;
        }
}
console.log(detectPrime([15,110,7,22,25]));// true
console.log(detectPrime([15,110,77,290,20]));// false

i want to run a code that prints booleans(true) if any of the elements in the array contains a prime number. I’ve declared a prime number function but I want to insect it in the detect function and print the right output. how can I do this?

To display your code in here you need to wrap it in triple back ticks. On a line by itself type three back ticks. Then on the first line below the three back ticks paste in your code. Then below your code on a new line type three more back ticks. The back tick on my keyboard is in the upper left just above the Tab key and below the Esc key.

I would start with your checkPrime function. What is it supposed to return? Does it return that?

Then your detectPrime function. Don’t you need start at the beginning of arr and start testing numbers to see if any of them are primes? How would you do that?

Tge checkprime function is return prime numbers, 1,2,3,5,7…

For the detectprime number, i was expecting to get the correct output when i loop over the array. But im still not getting the right output

A return statement immediately stops your function.

Hmm…what do you suggest i do/change in the code

You aren’t printing anything, did you mean to print or return?

In any case, if you don’t want to stop the function early, then you need to think where to put your return statements.

Can you say inside of the loop that none of the numbers are a prime?

1 Like

[15, 110, 7, 22, 25] …true

[15, 110, 77, 290, 20] . …false

The first array contains a prime number (7) and it should return true, while the second does not contain prime and array should return false

I understand how the function is supposed to work (except the print vs return I pointed out above).

I’m asking you, can you say in the middle of the loop that none of the other numbers in the array are prime? The loop body only handles one iteration at a time.

Currently, the checkPrime function doesn’t explicitly return anything, which means it always returns undefined. You need to fix this first.

In the detectPrime function you are calling checkPrime without passing in any arguments. It looks like checkPrime is supposed to be passed in a number.

2 Likes

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