1// This seems to be the most common and comprehensive means
2// of implementing a 'is a number a prime function' in javascript
3// if you would like t0 learn more complex , but efficient algorithms.
4// Visit the link below
5// https://stackoverflow.com/questions/11966520/how-to-find-prime-numbers-between-0-100
6
7function isPrime(num) {
8 if(num < 2) return false;
9
10 for (let k = 2; k < num; k++){
11 if( num % k == 0){
12 return false;
13 }
14 }
15 return true;
16}
1// program to check if a number is prime or not
2function isPrime(){
3 // take input from the user
4 const number = parseInt(prompt("Enter a positive number: "));
5 let isPrime = true;
6
7 // check if number is equal to 1
8 if (number === 1) {
9 console.log("1 is neither prime nor composite number.");
10 }
11
12 // check if number is greater than 1
13 else if (number > 1) {
14
15 // looping through 2 to number-1
16 for (let i = 2; i < number; i++) {
17 if (number % i == 0) {
18 isPrime = false;
19 break;
20 }
21 }
22
23 if (isPrime) {
24 console.log(`${number} is a prime number`);
25 } else {
26 console.log(`${number} is a not prime number`);
27 }
28 }
29
30 // check if number is less than 1
31 else {
32 console.log("The number is not a prime number.");
33 }
34}