Q:

Function supposed to return a boolean returns undefined

Trying to come up with a function to check if a number is prime and I'm running into trouble. I'm sure there's a simpler way to do this, but why would this function not return false, for the number 9? It returns false for even numbers but for any other type of composite number it returns undefined, but since it prints NOT PRIME it should also be returning false.
function isPrime(n, i) {
    document.writeln(i);
    var nextNum = i + 1;
    var number = n;
    if (i < n) {
        if ((n % i) === 0) {
            document.writeln("NOT PRIME");
            return false;
        } else {
            document.writeln(nextNum);
            isPrime(number, nextNum);
        }
    } else if (i === n) {
        document.writeln("Recursion ends");
        return true;
    } else {
        document.writeln("Confused" + typeof i + typeof n);
    }
}

A:

You need to return the value of the recursive call, i.e., change
isPrime(number, nextNum);

to
return isPrime(number, nextNum);

