Skip to content

Commit 1fc6a1a

Browse files
committed
[Add] Example of checking if a num is prime
1 parent 741149f commit 1fc6a1a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const prompt = require("prompt-sync")();
2+
function checkPrime(num) {
3+
if (num < 0 || num == 1) {
4+
return false
5+
}
6+
let i = 2;
7+
const myNum = Math.floor(num / 2) + 1; // Divide by 2 and round down
8+
for (i; i < myNum; i++) {
9+
10+
if (num % i === 0) {
11+
return false;
12+
}
13+
}
14+
console.log(`${num} is prime`);
15+
return true;
16+
}
17+
let userInp;
18+
let maxIterations = 10; // or any other number you like
19+
let i = 0;
20+
while (userInp !== 0 && i < maxIterations) {
21+
userInp = parseInt(prompt("Enter number (or 0 to stop) "));
22+
if (userInp !== 0) {
23+
console.log(checkPrime(userInp));
24+
}
25+
i += 1;
26+
}
27+
28+

0 commit comments

Comments
 (0)