We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 741149f commit 1fc6a1aCopy full SHA for 1fc6a1a
Tutorials/Basic/loops/for_loops/03_isprime.js
@@ -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