Skip to content

Commit 2f1fb03

Browse files
committed
fixed my code to be any number
1 parent 76abc20 commit 2f1fb03

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

lesson_04/dasiaenglish/lesson_04.md

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
## Javascript
2-
```javascript
3-
function findPrimes() { // A machine that helps find prime numbers
4-
let primeNumbers = []; // Use 'let' and an empty array for prime numbers
1+
```Javascript
2+
#Javascript
3+
4+
const readline = require(`readline`);
5+
6+
const rl =readline.createInterface({
7+
input: process.stdin,
8+
output: process.stdout
9+
});
10+
11+
function findPrimes(numberToCheck) { // A machine that helps find prime numbers
12+
13+
if (numberToCheck <=1){
14+
return `${numberToCheck} is not a prime number.`; //any number that is less or equal to 1 it is NOT a prime number
15+
}
16+
let isPrime = true; //I am start with assuming the number is prime
517

6-
for (let numberToCheck = 2; numberToCheck <= 100; numberToCheck++) { // Here is a loop that starts at 2 and keeps going until 100
7-
let isPrime = true; //I am starting this out with assuming the number is true
818

919
for (let factor = 2; factor <= Math.floor(numberToCheck / 2); factor++) { //this is another loop but it checks to see if the number is divisible by other numbers.
1020
if (numberToCheck % factor === 0) { // this is checking to see if the number can divide evenly and if so then it is not a prime number
@@ -14,43 +24,48 @@ function findPrimes() { // A machine that helps find prime numbers
1424
}
1525

1626
if (isPrime) { //if said number is still true that means that we did not find any number that is divided evenly so it is prime
17-
primeNumbers.push(numberToCheck); // Push primes into the array box above []
18-
}
19-
} //closing the loop from 2 to 100
27+
return `${numberToCheck} is a prime number.`; //if the numbe is prime it will say^^
28+
} else{
29+
return `${numberToCheck} is not a prime number.`; // if it is NOT prime it will say so
30+
}
31+
} //closing the loop of if it is a prime number or not
2032

33+
rl.question(`Enter a number to check if it\'s prime:`, (input)=>{
34+
let number = parseInt(input);
2135

22-
//Output the prime numbers
23-
console.log("Prime numbers from 1 to 100 are:"); // telling the comuter to print it out what is in " "
24-
console.log(primeNumbers.join(", ")); // show the prime numbers that we found
25-
} //will fill in all the numbers that are prime
36+
if (isNaN(number)) {
37+
console.log("Please enter a valid number.");
38+
} else {
39+
console.log(findPrimes(number));
40+
}
41+
});
42+
// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0 and Chatgpt for a explanation of things I still might have been confused about
43+
```
2644

27-
// Call the function to find and print primes
28-
findPrimes();
45+
```python
46+
# Python
47+
# this is a function that will help us find all the prime numbers
48+
def find_primes(number_to_check):
49+
if number_to_check <= 1: # this is an empty list for now until we run the test for all the prime numbers we find
50+
return f"{number_to_check} is not a prime number."
2951

30-
// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0
31-
```
52+
is_prime = True # I am saying that it is a prime until I find out it is not
3253

54+
# checks to see if the number can be divided by a smaller number evenly
55+
for factor in range(2, number_to_check // 2 + 1):
56+
if number_to_check % factor == 0: # trying to see if there is a remainder after the divison and if it is equal to zero
57+
is_prime = False # if it is equal to zero it is flase meaning it is not prime
58+
break # again it means STOP
59+
if is_prime: # after checking all
60+
return f"{number_to_check} is a prime number."
61+
else:
62+
return f"{number_to_check} is not a prime number."
63+
64+
number = int(input("Enter a number to check to see if its prime: "))
65+
print(find_primes(number))
3366

34-
## Python
35-
```python
36-
def find_primes(): # this is a function that will help us find all the prime numbers
37-
prime_numbers = [] # this is an empty list for now until we run the test for all the prime numbers we find
38-
39-
for number_to_check in range(2, 101): # checking numbers from 2 to 100
40-
is_prime = True # I am saying that it is a prime until I find out it is not
41-
42-
for factor in range(2, number_to_check // 2 + 1): # checks to see if the number can be divided by a smaller number evenly
43-
if number_to_check % factor == 0: # trying to see if there is a remainder after the divison and if it is equal to zero
44-
is_prime = False # if it is equal to zero it is flase meaning it is not prime
45-
break # again it means STOP
46-
if is_prime: # after checking all
47-
prime_numbers.append(number_to_check) # Add prime numbers
48-
49-
print("Prime numbers from 1 to 100 are:")
50-
# type out what is in the " "
51-
print(", ".join(map(str, prime_numbers))) # put all the prime numbers split by a commma
52-
find_primes() #tells the computer to go ahead and run the function
5367
```
68+
5469
## Explanation
5570
My first thought was to use Javascript and html because those were the 2 languages that I was familiar with. I did some research and quickly came to the realization that html would not be the most effective. That's when I found out that I should use Python and Javascript.
5671
Python is known for how easy it is to read and how simple it is. But is super space indentation sensitive. Whereas Javascript is a little more complex because it uses different characters, which makes it a little harder to understand.

0 commit comments

Comments
 (0)