Answer the question
In order to leave comments, you need to log in
How to understand this function?
Good afternoon, please explain on your fingers what is happening here?
confused
The function determines whether a number is prime or not
A prime number is a number that has 2 divisors, itself and one
const isPrime = (number) => {
if (number < 2) {
return false;
}
for ( let i = 2; i <= number / 2; i++) {
if (number % i === 0) {
return false;
}
}
return true;
}
document.write(isPrime(7))
Answer the question
In order to leave comments, you need to log in
With 7, they check whether it is divisible by 2 and by 3 - it is not divisible by either, which means that it is simple.
Why half. Because the second factor cannot be less than 2, only greater than or equal to.
The meaning of enumeration is to catch that, here, aha! - divisible by this number! — means, not simple and further it is possible not to check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question