File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments