Skip to content

Commit d3286fd

Browse files
authored
Update readme.md
1 parent ac8ef3b commit d3286fd

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# JavaScript-snippets
22
JavaScript Snippets
33

4+
5+
# How to generate a random number in a given range
6+
47
```javascript
5-
// Returns a random number between min (inclusive) and max (exclusive)
8+
// Returns a random number(float) between min (inclusive) and max (exclusive)
69

710
const getRandomNumber = (min, max) => Math.random() * (max - min) + min;
811

912
getRandomNumber(2, 10)
1013

11-
// Returns a random number between min (inclusive) and max (inclusive)
14+
// Returns a random number(int) between min (inclusive) and max (inclusive)
1215

1316
const getRandomNumberInclusive =(min, max)=> {
1417
min = Math.ceil(min);
@@ -18,3 +21,43 @@ const getRandomNumberInclusive =(min, max)=> {
1821

1922
getRandomNumberInclusive(2, 10);
2023
```
24+
25+
# How to find the difference between two arrays.
26+
27+
28+
```javascript
29+
const firstArr = [5, 2, 1];
30+
const secondArr = [1, 2, 3, 4, 5];
31+
32+
const diff = [
33+
...secondArr.filter(x => !firstArr.includes(x)),
34+
...firstArr.filter(x => !secondArr.includes(x))
35+
];
36+
console.log('diff',diff) //[3,4]
37+
38+
39+
function arrayDiff(a, b) {
40+
return [
41+
...a.filter(x => b.indexOf(x) === -1),
42+
...b.filter(x => a.indexOf(x) === -1)
43+
]
44+
}
45+
console.log('arrayDiff',arrayDiff(firstArr, secondArr)) //[3,4]
46+
47+
48+
49+
50+
const difference = (a, b) => {
51+
const setA = new Set(a);
52+
const setB = new Set(b);
53+
54+
return [
55+
...a.filter(x => !setB.has(x)),
56+
...b.filter(x => !setA.has(x))
57+
58+
]
59+
};
60+
61+
difference(firstArr, secondArr); //[3,4]
62+
console.log('difference',difference(firstArr, secondArr))
63+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy