Skip to content

Commit d7d4d1b

Browse files
authored
feat: adds LJs code samples in JS and Java to find prime numbers (#142)
* Initial commit of program files. Created java scanner. WIP * finished java program. changed variable name for Scanner. added comments for clarity. * populated the JS file. Implemented Node.js and added comments for clarity * added explanation readme file
1 parent 8d30a82 commit d7d4d1b

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

lesson_04/ljmcwilliams/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
In this lesson, I explored the differences between Java and JavaScript. Despite their similar names, these languages have distinct features.
2+
3+
- ## Key Differences
4+
- ### Document Preparation:
5+
6+
- Java requires preparation before defining variables and functions.
7+
JavaScript has more flexibility with variable definitions.
8+
9+
- ### User Input:
10+
11+
- In Java, I used the Scanner class to prompt the user for number input.
12+
This required importing Java's Scanner class.
13+
In JavaScript, there is no built-in Scanner class, so I used Node.js as a workaround.
14+
With Node.js, I utilized process.stdin for input and process.stdout for output (similar to console.log).
15+
16+
- ### Loop Control:
17+
18+
- Both languages allow for loops to be exited early, but the methods differ:
19+
In Java, using a break statement within the loop will terminate it.
20+
In JavaScript, I opted for read.close(), which achieves a similar effect.
21+
22+
- ### Variable Declaration:
23+
24+
- Java requires explicit declaration of variable types.
25+
JavaScript automatically assigns variable types.
26+
- ## Loop Behavior
27+
Both languages share similarities in loop functionality:
28+
29+
- **For Loops**:
30+
- Take three expressions.
31+
32+
- **If Statements**:
33+
- Accept a condition and can be nested.
34+
35+
- **Java Example**
36+
```java
37+
for (int i = 2; i < userInput; i++) {
38+
if ((userInput / i) == (int) (userInput / i)) {
39+
System.out.println(userInput + " is not a prime number.");
40+
isConditionMet = true;
41+
break;
42+
}
43+
} if (!isConditionMet) {
44+
System.out.println(userInput + " is a prime number.");
45+
}
46+
```
47+
- **JavaScript Example**
48+
```javascript
49+
for (let i = 2; i < answer; i++) {
50+
if ((answer / i) == parseInt(answer / i)) {
51+
console.log(answer + " is not a prime number.");
52+
isConditionMet = true;
53+
read.close();
54+
}
55+
} if (!isConditionMet) {
56+
console.log(answer + " is a prime number.");
57+
read.close();
58+
}
59+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner; //This allows us to use the scanner to take in user input
2+
public class findPrimeNum {
3+
public static void main(String[] args) {
4+
Scanner objScanner = new Scanner(System.in); //A scanner named 'objScanner'
5+
boolean isConditionMet = false;
6+
7+
/*if the condition is met, the print statement within the loop will be printed.
8+
If not, the print statement after the loop will be printed*/
9+
10+
System.out.print("Please enter a whole number: "); //User input prompt
11+
double userInput = objScanner.nextDouble(); //This reads an integer value from the user
12+
13+
/*to determine a number's primality, we can check to see if the division
14+
of that number by 2 to (1 - that number) results in any whole number quotients.
15+
Once we do the division we can compare that number to (int) number to see if the
16+
values are equal.*/
17+
18+
/*the program should end if A: the first whole number quotient is found, indicating that
19+
* indicating a number is not prime or if B: no whole number quotients are found within the
20+
* range indicating the number IS prime.*/
21+
22+
for (int i = 2; i < userInput; i++) {
23+
if ((userInput / i) == (int) (userInput / i)) {
24+
System.out.println(userInput + " is not a prime number.");
25+
isConditionMet = true;
26+
break; //this helps jump out of the loop once this first whole number quotient instace is found
27+
}
28+
} if (!isConditionMet) {
29+
System.out.println(userInput + " is a prime number.");
30+
}
31+
}
32+
}
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)