|
1 | | -## Python implementation |
| 1 | +# Explanation |
2 | 2 |
|
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. |
6 | 4 |
|
7 | | -# Example usage: |
8 | | -print(is_even(4)) # Output: True |
9 | | -print(is_even(7)) # Output: False |
10 | | -``` |
| 5 | +--- |
11 | 6 |
|
12 | | -## JavaScript implementation |
| 7 | +## Similarities |
13 | 8 |
|
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: |
18 | 10 |
|
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. |
23 | 12 |
|
24 | | -## Explanation |
| 13 | +* **Skipping Even Numbers:** After checking for 2, both functions say that any other even number is not prime. |
25 | 14 |
|
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. |
27 | 16 |
|
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. |
29 | 18 |
|
30 | | -### Differences |
| 19 | +--- |
31 | 20 |
|
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 |
35 | 22 |
|
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