Skip to content

Commit 993b735

Browse files
feat(prime-checker): add JavaScript isPrime function
1 parent 3412a05 commit 993b735

File tree

4 files changed

+85
-38
lines changed

4 files changed

+85
-38
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

lesson_04/jonee_prime.py

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Python implementation
2+
3+
```python
4+
def is_even(number):
5+
return number % 2 == 0
6+
7+
# Example usage:
8+
print(is_even(4)) # Output: True
9+
print(is_even(7)) # Output: False
10+
```
11+
12+
## JavaScript implementation
13+
14+
```javascript
15+
function isEven(number) {
16+
return number % 2 === 0;
17+
}
18+
19+
// Example usage:
20+
console.log(isEven(4)); // Output: true
21+
console.log(isEven(7)); // Output: false
22+
```
23+
24+
## Explanation
25+
26+
The Python implementation uses a function named `is_even` that takes a single argument `number`. It returns `True` if the number is even (i.e., when the remainder of the division of the number by 2 is zero), otherwise, it returns `False`.
27+
28+
The JavaScript implementation uses a function named `isEven` that also takes a single argument `number`. It returns `true` if the number is even (using the same logic as the Python function) and `false` otherwise.
29+
30+
### Differences
31+
32+
1. **Syntax**:
33+
- In Python, functions are defined using the `def` keyword, whereas in JavaScript, the `function` keyword is used.
34+
- Python uses `True` and `False` for boolean values, while JavaScript uses `true` and `false`.
35+
36+
2. **Type Coercion**:
37+
- JavaScript has type coercion, which can sometimes lead to unexpected results if the input is not properly handled. In contrast, Python is more strict with types.
38+
39+
3. **Function Calls**:
40+
- The syntax for calling functions and printing to the console/output is slightly different. Python uses `print()`, while JavaScript uses `console.log()`.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Determines if a given number is a prime number.
3+
*
4+
* A prime number is a natural number greater than 1 that has no positive divisors
5+
* other than 1 and itself.
6+
*
7+
* @param {number} number The number to check.
8+
* @returns {boolean} True if the number is prime, False otherwise.
9+
*/
10+
function isPrime(number) {
11+
// In JavaScript, numbers less than or equal to 1 are not prime
12+
if (number <= 1) {
13+
return false;
14+
}
15+
// 2 is the only even prime number
16+
if (number === 2) {
17+
return true;
18+
}
19+
// Other even numbers are not prime (using strict equality for comparison)
20+
if (number % 2 === 0) {
21+
return false;
22+
}
23+
24+
// Check for divisors from 3 up to the square root of the number,
25+
// incrementing by 2 to only check odd numbers.
26+
// Math.sqrt() is used for efficiency.
27+
for (let i = 3; i <= Math.sqrt(number); i += 2) {
28+
// If a divisor is found, the number is not prime
29+
if (number % i === 0) {
30+
return false;
31+
}
32+
}
33+
// If no divisors were found, the number is prime
34+
return true;
35+
}
36+
37+
// Example usage:
38+
// Use console.log() to print output in JavaScript
39+
console.log(`Is 7 prime? ${isPrime(7)}`); // Output: true
40+
console.log(`Is 10 prime? ${isPrime(10)}`); // Output: false
41+
console.log(`Is 2 prime? ${isPrime(2)}`); // Output: true
42+
console.log(`Is 1 prime? ${isPrime(1)}`); // Output: false
43+
console.log(`Is 13 prime? ${isPrime(13)}`); // Output: true
44+
console.log(`Is 49 prime? ${isPrime(49)}`); // Output: false

0 commit comments

Comments
 (0)