Skip to content

Commit b98406d

Browse files
yafiahaYafiaha
andauthored
Feat: added lesson-04 isPrimeNumber function Java & JavaScript (#137)
* Feat: added lesson-03 isPrimeNumber function Java & JavaScript * Feat: added description * Feat: added lesson-03 isPrimeNumber function Java & JavaScript Feat: added description * Feat: added lesson-03 isPrimeNumber function Java & JavaScript Feat: added description Feat: added description --------- Co-authored-by: Yafiaha <[email protected]>
1 parent a5a93b0 commit b98406d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lesson_04/yafiahabdullah/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Javascript isPrimeNumber
2+
3+
```javascript
4+
function isPrimeNumber(number){
5+
let result= "not prime";
6+
if(number <= 1){
7+
return result;
8+
}
9+
for(let i = 2; i < number; i++ ){
10+
if(number % i === 0){
11+
return result;
12+
}
13+
}
14+
return "prime number";
15+
}
16+
# Example usage:
17+
console.log(isPrimeNumber(4)) # Output: false
18+
console.log(isPrimeNumber(7)) # Output: true
19+
```
20+
21+
## Explanation
22+
23+
The JavaScript function isPrimeNumber checks if a given number is prime. It initializes a result variable with "not prime" and first checks if the number is less than or equal to 1, returning this result if true. Then, it loops from 2 up to the number, checking for divisibility. If the number can be evenly divided by any value in this range, it returns "not prime". If no divisors are found, the function concludes that the number is prime and returns "prime number"
24+
25+
26+
## Java isPrimeNumber implementation
27+
28+
```java
29+
public class PrimeChecker {
30+
public static String isPrimeNumber(int number) {
31+
if (number <= 1) {
32+
return "not prime number";
33+
}
34+
for (int i = 2; i < number ; i++) {
35+
if (number % i == 0) {
36+
return "not prime number";
37+
}
38+
}
39+
return "prime number";
40+
}
41+
}
42+
# Example usage:
43+
System.out.println(isPrimeNumber(4)) # Output: false
44+
System.out.println(isPrimeNumber(7)) # Output: true
45+
```
46+
47+
## Explanation
48+
49+
The Java implementation uses a function named `isPrimeNumber` This function effectively determines if a number is prime by checking its divisibility with all integers from 2 up to that number, providing clear output based on the result.
50+
51+
### Differences
52+
53+
1.
54+
Overall, while both languages share some similar structural elements (like curly braces for blocks), Java has stricter syntax rules regarding types and class structure, whereas JavaScript is more flexible with its dynamic typing and function definitions.
55+
56+

0 commit comments

Comments
 (0)