Skip to content

Commit 16719cf

Browse files
committed
populated the JS file. Implemented Node.js and added comments for clarity
1 parent fe46208 commit 16719cf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const readline = require('readline'); //"require()" imports the readline module which will process the user input
2+
const read = readline.createInterface({
3+
input: process.stdin, //this acts as an input scanner
4+
output: process.stdout //this works like a normal console.log combined with a user's input
5+
});
6+
let isConditionMet = false;
7+
8+
/* When looking at the Node.js docs, it states '.question()' displays the query (in this case the user prompt).
9+
It waits for an input and uses it as the first argument in a callback function (here I am using a arrow function). */
10+
read.question("Enter a number: ", (answer) => {
11+
for (let i = 2; i < answer; i++) {
12+
if ((answer / i) == parseInt(answer / i)) { //similar to the "(int)" statement in java, parseInt converts doubles to ints
13+
console.log(answer + " is not a prime number.");
14+
isConditionMet = true;
15+
read.close(); //this works similar to a break statement in java and ends the program
16+
}
17+
} if (!isConditionMet) {
18+
console.log(answer + " is a prime number.");
19+
read.close();
20+
}
21+
})

0 commit comments

Comments
 (0)