Prime Directive project

Hi guys. I have I have a question. I did the project Prime Directive project(https://www.codecademy.com/courses/learn-java/projects/java-prime-directive).
This is my code import java.util.ArrayList;

class PrimeDirective {

public boolean isPrime(int number) {
if (number == 2) {
return true;
} else if (number < 2) {
return false;
}

for (int i = 2; i < number; i++) {
  if (number % i == 0) {
    return false;
  }
}
return true;

}

public static void main(String args) {

PrimeDirective pd = new PrimeDirective();
int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
System.out.println(pd.isPrime(6));

}

}
it works fine but i dont understand why isPrime function should return true? I first made my code without returning anything because I didn’t know what to put. By setting true I have a code that works but I do not understand how. Thanks for taking the time to respond

If your function returns true, it means for every i, number % i == 0 was not true. So what does that imply? (your answer lies in the answer to that question)

Thanks for taking time to respond to me. so if i test multiple values ​​if i set true for the conditions i want to be valid i have to return true? Does this mean that when the conditions are not validated in the function nothing is returned? I have difficulties to understand ? If a number is not a prime then it is not true which is returned at the end? However the return true is after the brace of the previous loops. This would mean that the return does not take into account the previous conditions. Sorry to bother you with my questions but I’ve been stuck on this since yesterday.

So let’s analyze your loop, which is the centerpiece of this code.

The loop only runs on the interval [2, n-1] (inclusive) such that n is the number you are testing (since (n-1) is where your loop ends).

It basically checks if each number from 3 to (n-1) divides n. In which case, it has a divisor in this range and is not prime.

If it doesn’t, this implies the only positive integer divisors to n are 1 and itself, the definition of a prime number.

If you understand this logic, and your code is returning wrong values… then question is: at what juncture of the code is something going wrong?

One strategy you can use is to explicitly print out a statement that says what number and i are in your loop to see if it’s acting like what you envision.

Also remember that since java is a compiled language, every time you make a change to your code you need to re-compile to test the new changes (otherwise you’re testing old code).