lundi 10 août 2020

I cannot understand this specific syntax of the RETURN instruction within the IF argument

I am learning JS through some exercises. One of them is about writing a function that checks whether a number is prime or not. The correct answer to this exercise is this one:

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

I'm accustomed to inserting return false; inside the if argument and the return num > 1;or return true; outside of it, right after the closing bracket that ends the if argument, as with the if statement I am usually telling to my function "if this condition fulfilled, return this" and right after having closed the if statement I'm telling it "...otherwise return that". I cannot understand why in this case both the return instructions have be inserted inside the argument, what's the logic behind that?

Aucun commentaire:

Enregistrer un commentaire