Skip to content

Commit af271a4

Browse files
feat: Mercedes Lesson04 ReadMe (#147)
1 parent 915afa7 commit af271a4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

lesson_04/mercedesmathews/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Python implementation
2+
```python
3+
def is_prime(num):
4+
if num < 2:
5+
return False
6+
else:
7+
for i in range(2, int(num ** 0.5) + 1):
8+
if num % i == 0:
9+
return False
10+
return True
11+
```
12+
13+
## JavaScript implementation
14+
```javascript
15+
function isPrime(num) {
16+
if (num < 2) {
17+
return false;
18+
} else {
19+
for (let i = 2; i * i <= num; i++) {
20+
if (num % i === 0) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
}
27+
```
28+
29+
## Explanation
30+
The Python implementation uses a function named `is_prime` and takes a single argument `num` and returns `true` if the number is a prime number (i.e., when the number is only divisible by 1 and itself), otherwise, it returns `false`.
31+
32+
The JavaScript implementation uses a function named `isPrime` and takes a single argument `num` and returns `True` if the number is a prime number (i.e., when the number is only divisible by 1 and itself), otherwise, it returns `False`.
33+
34+
35+
### Similarities
36+
- Input
37+
- Both languages don't need the parameter datatype declared.
38+
39+
- Conditionals
40+
- Both languages have `if` and `else` statements.
41+
42+
### Differences
43+
- Syntax:
44+
- To define a function, Python uses the keyword `def` while JavaScript uses the keyword `function`.
45+
- Python uses a colon `:` and indentation to determine code blocks, while JavaScript uses curly brackets `{}`.
46+
- JavaScript uses a semicolon `;` at the end of a statement.
47+
48+
- Input/Output:
49+
- Python's boolean values are `True` and `False`, while JavaScript's are `true` and `false`.
50+
- To print output, Python uses `print()`, while JavaScript uses `console.log()`.
51+
52+
- Conditionals and Loops
53+
- JavaScript has an `else if` statement, while Python has an `elif` statement.
54+
- JavaScript `for loops` have three expressions to determine loop, while Python loops based off a given range.
55+

0 commit comments

Comments
 (0)