In JavaScript, how would I generate 100 random numbers and display the results of checking each one for primality, if I'm using a Boolean function named isPrime which then takes an integer as an argument and returns true if the argument is a prime number of false? For some reason I am stuck..
So far I have this:
function isPrime(number) {
var start = 2;
while (start <= Math.sqrt(number)) {
if (number % start++ < 1) return false;
}
return number > 1;
}
Use the
Math.random()
function. I assume you want random integers in some interval, so you can use a function like this:To generate, say, 100 random numbers between 0 and 250, you could call
Loop through the random numbers you generated and use your
isPrime
function to check each one for primality: