Skip to content

Commit c4a2b7e

Browse files
authored
feat: add Meiko's code samples (#148)
1 parent 7438405 commit c4a2b7e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

lesson_04/meikostephens/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Python implementation
2+
3+
```python
4+
def is_prime(number):
5+
if number <= 1:
6+
return False # Numbers less than or equal to 1 are not prime
7+
# Check divisibility up to the square root of the number
8+
for i in range(2, int(number ** 0.5) + 1):
9+
if number % i == 0: # If number is divisible by i, it's not prime
10+
return False
11+
return True # If no divisors were found, the number is prime
12+
13+
14+
# Example usage:
15+
num = int(input("Enter a number: "))
16+
if is_prime(num):
17+
print(f"{num} is a prime number.")
18+
else:
19+
print(f"{num} is not a prime number.")
20+
21+
```
22+
## JavaScript implementation
23+
24+
```javascript
25+
function isPrime(number) { if (number <= 1) {
26+
return false; // Numbers less than or equal to 1 are not prime
27+
}
28+
for (let i = 2; i <= Math.sqrt(number); i++) { // Check divisibility up to the square root of the number
29+
if (number % i === 0) {
30+
return false; // If number is divisible by i, it's not prime
31+
}
32+
}
33+
return true; // If no divisors are found, the number is prime
34+
}
35+
36+
// Example usage:
37+
let num = parseInt(prompt("Enter a number:"));
38+
if (isPrime(num)) {
39+
console.log(num + " is a prime number.");
40+
} else {
41+
console.log(num + " is not a prime number.");
42+
}
43+
```
44+
## Explanation
45+
46+
The Python implementation uses a function named `is_prime` that takes a single argument `number`. It returns `True` if the number is prime otherwise, it returns `False`.
47+
48+
The JavaScript implementation uses a function named `is_Prime` that also takes a single argument `number`. It returns `true` if the number is prime (using the same logic as the Python function) and `false` otherwise.
49+
50+
Both implementations uses `num` and the strings: `"Enter a number"`,`"is a prime number"`&`"is not a prime number"` in the
51+
code.
52+
Both implementations uses `if`, `else`, `for` and `return` to make the code function correclty.
53+
54+
### Differences
55+
56+
1. **Syntax**:
57+
- In Python, functions are defined using the `def` keyword, whereas in JavaScript, the `function` keyword is used.
58+
- Python uses `True` and `False` for boolean values, while JavaScript uses `true` and `false`.
59+
- Python uses `int (input())` to allow the integer input, while JavaScript uses `parseIn(prompt())`.
60+
61+
2. **Type Coercion**:
62+
- 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.
63+
64+
3. **Function Calls**:
65+
- The syntax for calling functions and printing to the console/output is slightly different. Python uses `print()`, while JavaScript uses `console.log()`.
66+

0 commit comments

Comments
 (0)