[Solved] Please someone please explain me the difference between the below mention functions


The first one will stop straight after the first iteration, because of the else statement. return breaks the loop. It will only test if the number you give is even or odd num%2 == 0.

The second one will stop only if the condition is verified (again, the return breaks the loop), or at the end of all iterations.

In your example, only the second one gives the expected result:

function isPrimeA(num) {
    if(num < 2) return false;
    for (var i = 2; i < num; i++) {
        if(num%i==0){
            return false;
        }else{
            return true;
        }
    }
}

function isPrimeB(num) {
    if(num < 2) return false;
    for (var i = 2; i < num; i++) {
        if(num%i==0)
            return false;
    }
    
    return true;
}

console.log(isPrimeA(15))
console.log(isPrimeB(15))

solved Please someone please explain me the difference between the below mention functions