Skip to content

Commit 1e1968a

Browse files
docs: add README
1 parent 993b735 commit 1e1968a

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed
Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
1-
## Python implementation
1+
# Explanation
22

3-
```python
4-
def is_even(number):
5-
return number % 2 == 0
3+
We were able to build the same tool in two different languages! Even though they look a bit different, Python `is_prime` function and JavaScript `isPrime` function actually have a lot in common.
64

7-
# Example usage:
8-
print(is_even(4)) # Output: True
9-
print(is_even(7)) # Output: False
10-
```
5+
---
116

12-
## JavaScript implementation
7+
## Similarities
138

14-
```javascript
15-
function isEven(number) {
16-
return number % 2 === 0;
17-
}
9+
At their core, both functions do the same job, they figure out if a number is prime or not. They both use the same **logic** to do this, which is important for finding prime numbers efficiently:
1810

19-
// Example usage:
20-
console.log(isEven(4)); // Output: true
21-
console.log(isEven(7)); // Output: false
22-
```
11+
* **Handling Small Numbers:** Both codes know that numbers less than or equal to 1 are not prime. They also both correctly identify that the number 2 is the only even prime number.
2312

24-
## Explanation
13+
* **Skipping Even Numbers:** After checking for 2, both functions say that any other even number is not prime.
2514

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`.
15+
* **Checking Divisors Efficiently:** Both use a loop that goes up to the square root of the number. You only need to check for divisors up to that point. If a number has a divisor larger than its square root, it must also have one smaller than its square root, which would have already been found.
2716

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.
17+
* **Returning True/False:** Both functions return a true or false answer to tell you if the number is prime.
2918

30-
### Differences
19+
---
3120

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`.
21+
## Differences
3522

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()`.
23+
While the core function is the same, Python and JavaScript have their own rules, which lead to these differences:
24+
25+
* **How You Start a Function:**
26+
27+
* In Python, you say `def` to start a function, like `def is_prime(number)`.
28+
29+
* In JavaScript, you say `function` to start one, like `function isPrime(number) {`. There are curly braces `{}` in JavaScript to show where the function's code begins and ends, whereas Python uses indentation.
30+
31+
* **True/False Words:**
32+
33+
* Python uses `True` and `False` with capital first letters.
34+
35+
* JavaScript uses `true` and `false` all lowercase.
36+
37+
* **Printing Things Out:**
38+
39+
* To show results in Python, you use `print()`.
40+
41+
* In JavaScript, you use `console.log()`.
42+
43+
* **Variable Setup:**
44+
45+
* In Python, you just name a variable and give it a value.
46+
47+
* In JavaScript, you often use `let` or `const` when you first introduce a variable.

0 commit comments

Comments
 (0)