Skip to content

Commit 581722d

Browse files
AmiyahJoAmiyahJo
andauthored
feat: adds Amiyah Jones , lesson 04 : two languages (#138)
* Create README.md Accidentally removed my work the 1st time by merging * Delete lesson_00/amiyahjones/README.md * create file and readme * READMe.md draft & thought proccess * README.md manual test * getting there...just thinking * README.md explanation * ran tests and this isn't a valid code. will re-try * fix up Feedback from chatgpt simply checking if there is any remainders left * Update README.md * update comments to README.md * explain my javascript function in README.md * React addition to README.md If you check my repositories, I have a few projects back from the youth program where we learned about how to use react. I hope it counts since I'm a bit familiar with at least using a function and importing the 'use state'. I can recall it's almost like javascript but easier(?) * React code in README.md Snippets inspired off of my previous ones: https://github.com/AmiyahJo/mega-dashboard-24/blob/master/mega-dash/src/components/DiceRoller.jsx , and https://github.com/AmiyahJo/Worlde-24-Project/blob/main/src/App.jsx. Along with chatgpt's help on making the App function to submit and include other features * explanatinon to react README.md * simple explanations README.md * remove react README.md I forgot react is a javascript library, not a programming language. * sneaky html joke. shh README.md * slowly transforming html to ruby. Looking through tutorials. * ruby prime checker! a 9 year old tutorial https://www.youtube.com/watch?v=33pLqGvk-PM explaining how to use a function what each of these do * ruby addition!! README.md * Differences + update README.md * Video credit for ruby README.md --------- Co-authored-by: AmiyahJo <[email protected]>
1 parent b98406d commit 581722d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

lesson_04/amiyahjones/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Javascript
2+
``` javascript
3+
4+
function isPrime(num) {
5+
if (num < 2) {
6+
return false; // Numbers less than 2 are not prime
7+
}
8+
9+
for (let i = 2; i < num; i++) {
10+
if (num % i === 0) {
11+
return false;
12+
}
13+
}
14+
15+
return true;
16+
}
17+
18+
console.log(isPrime(5)); //true
19+
console.log(isPrime(1)); //false
20+
21+
```
22+
## Ruby
23+
###### Tutorial used : https://www.youtube.com/watch?v=33pLqGvk-PM
24+
``` ruby
25+
def prime?(n)
26+
# '2..n-1' checks all the numbers from 2 up to less than 'n'.
27+
# 'none' -> sees if none of these numbers can divide 'n' evenly.
28+
(2..n-1).none? {|divisor| n % divisor == 0}
29+
# If none of these numbers divide 'n' without a remainder,
30+
# then 'n' is a prime number. Otherwise, it's not.
31+
end
32+
33+
p prime? 5 # prints 'true'
34+
p prime? 6 # prints 'false'
35+
p prime? 7 # also 'true'
36+
```
37+
38+
39+
## Explanation
40+
41+
My JavaScript implementation uses a function named ```isPrime``` that checks if a number is prime by first returning `false` if the number is less than 2, since prime numbers are greater than 1. Then, it tests every number from 2 up to one less than the given number to see if any of them can divide it without leaving a remainder. If it finds one that can divide it evenly, it returns `false` ; if not, it returns `true`, meaning the number is prime.
42+
43+
The ruby implementation has a function named ```prime?``` that also looks at all the numbers from 2 up to one less than the number given. Same rules apply: If none of those numbers can divide it evenly, then it prints ```true```. For example, it tells us if 5 and 7 are prime (they are!) and if 6 is prime (it’s not!).
44+
45+
### Differences
46+
1. Instead of ```function``` used to define what your function is in javascript, Ruby use ```def```
47+
2. Ruby is much more simple - where tthe ```if``` and ```else``` statement is simply in one line
48+
3. Ruby simply 'prints' the result of its functions rather than ```console.log();```
49+
50+

0 commit comments

Comments
 (0)