这是怎么回事?你能一步一步地解释一下吗?
// function to show primes
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
console.log(i); // a prime
}
}
// function to check prime
function isPrime(n) {
for (let i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
// trigger to run the function and put the value of (n)
showPrimes(10);
发布于 2019-03-12 03:53:25
// function declaration
function showPrimes(n) {
//For loop from first prime number to number of prime numbers to be printed (10)
for (let i = 2; i < n; i++) {
// Condition to check if a number is prime or not
if (!isPrime(i))
continue; //if it is not prime, then loop will continue to next iteration
console.log(i); // a prime will be shown
}
}
// Function declaration
function isPrime(n) {
//For loop to iterate till the given number that is to be checked for being prime or not
for (let i = 2; i < n; i++) {
// If in any case the number is divisible by any number between 2 to n, then its not prime
if (n % i == 0)
return false;
}
return true; // no need for an else after a return
}
showPrimes(10); // execute
https://stackoverflow.com/questions/55109273
复制相似问题