当其为质数返回true,否则返回false

function isPrime(number) {
    //Number.isInteger 是 ECMAScript 6的新方法
    if (typeof number !== 'number' || !Number.isInteger(number)) {
        return false;
    }
    if (number < 2) {
        return false;
    }
    if (number === 2) {
        return true;
    } else if (number % 2 === 0) {
        return false;
     }
    var squareRoot = Math.sqrt(number);
    for(var i = 3; i <= squareRoot; i += 2) {
        if (number % i === 0) {
            return false;
        }
    }
    return true;
}

你可能感兴趣的:(web前端,质数返回true,否则返回false)