Skip to content

Commit 776bf68

Browse files
committed
Update README.md
1. Fix typo. 2. Added "Shuffle Array" snippet. 3. Added "Find max number in array" snippet. 4. Added "Most frequent element in an array" snippet.
1 parent 5dc4eaf commit 776bf68

File tree

1 file changed

+62
-23
lines changed

1 file changed

+62
-23
lines changed

README.md

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
|3 | [Convert truthy/falsy to boolean(true/false)](#Convert-truthy-falsy-to-boolean)|
1212
|4 | [Repeat a string](#Repeat-a-string)|
1313
|5 | [Check how long an operation takes](#Check-how-long-an-operation-takes)|
14-
|6 | [Two ways to remove an item in a specific in an array](#Two-ways-to-remove-an-item-in-a-specific-in-an-array)|
14+
|6 | [Two ways to remove an item in a specific index in an array](#Two-ways-to-remove-an-item-in-a-specific-index-in-an-array)|
1515
|7 | [Did you know you can flat an array?](#Did-you-know-you-can-flat-an-array)|
1616
|8 | [Get unique values in an array](#Get-unique-values-in-an-array)|
1717
|9 | [Copy Text to Clipboard](#Copy-Text-to-Clipboard)|
@@ -54,6 +54,9 @@
5454
|46 | [get device orientation](#get-device-orientation)|
5555
|47 | [CONST vs LET vs VAR](#const-let-var)|
5656
|48 | [slice array with steps (like python)](#slice-array-with-steps-like-python)|
57+
|49 | [Shuffle Array](#shuffle-array)|
58+
|50 | [Find max number in array](#find-max-number-in-array)|
59+
|51 | [Most frequent element in an array](#most-frequent-element-in-an-array)|
5760

5861
**[⬆ Back to Top](#table-of-contents)**
5962
### How to generate a random number in a given range
@@ -162,12 +165,12 @@ console.log("this doSomething took " + (endTime - startTime) + " milliseconds.")
162165
```
163166

164167
**[⬆ Back to Top](#table-of-contents)**
165-
### Two ways to remove an item in a specific in an array
168+
### Two ways to remove an item in a specific index in an array
166169

167170
```javascript
168171
// Mutating way
169172
const muatatedArray = ['a','b','c','d','e'];
170-
muatatedArray.splice(2,1)
173+
muatatedArray.splice(2, 1)
171174
console.log(muatatedArray) // ['a', 'b', 'd', 'e']
172175

173176
// Non-mutating way
@@ -200,7 +203,7 @@ myArray.flat(infinity) // [2, 3, 4, 5, 7, 7, 8, 9, 1, 1];
200203
const numbers = [1, 1, 3, 2, 5, 3, 4, 7, 7, 7, 8];
201204

202205
// Ex1
203-
const unieqNumbers = numbers.filter((v,i,a) => a.indexOf(v )=== i )
206+
const unieqNumbers = numbers.filter((v,i,a) => a.indexOf(v) === i )
204207
console.log(unieqNumbers) // [1, 3, 2, 5, 4, 7, 8]
205208

206209
// Ex2
@@ -789,27 +792,26 @@ const reduceRightArray = arr.reduceRight((acc, current) => {
789792
**[⬆ Back to Top](#table-of-contents)**
790793
### Abort Fetch
791794
792-
```javascript
793-
794-
795-
// HTML
795+
```html
796+
<!-- HTML -->
796797
<button id="download">Download</button>
797798
<button id="abort">Abort</button>
798799
799-
// JS
800-
let controller;
801-
802-
document.querySelector('#download').addEventListener('click', () => {
803-
controller = new AbortController();
804-
const signal = controller.signal;
805-
fetch('https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4', {signal})
806-
.then(() => console.log('done'));
807-
});
808-
809-
document.querySelector('#abort').addEventListener('click', function() {
810-
controller.abort();
811-
});
812-
800+
<!-- JS -->
801+
<script type="text/javascript">
802+
let controller;
803+
804+
document.querySelector('#download').addEventListener('click', () => {
805+
controller = new AbortController();
806+
const signal = controller.signal;
807+
fetch('https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4', {signal})
808+
.then(() => console.log('done'));
809+
});
810+
811+
document.querySelector('#abort').addEventListener('click', function() {
812+
controller.abort();
813+
});
814+
</script>
813815
```
814816
815817
@@ -1010,7 +1012,7 @@ function getOrientation() {
10101012
**[⬆ Back to Top](#table-of-contents)**
10111013
### slice array with steps (like python)
10121014
1013-
```js
1015+
```javascript
10141016
10151017
Array.prototype.slice = function (start = 0, end = this.length, step = 1) {
10161018
const result = [];
@@ -1040,3 +1042,40 @@ console.log(array.slice(1, 9, 2)); // [1, 3, 5, 7]
10401042
console.log(array.slice(1, 9, -2)); // [7, 5, 3, 1]
10411043
10421044
```
1045+
1046+
**[⬆ Back to Top](#table-of-contents)**
1047+
### Shuffle Array
1048+
1049+
```javascript
1050+
// Three quick steps to shuffle an array in Javascript: Map to a random number, sort on that and map the object back!
1051+
const shuffleArray = anArray =>
1052+
anArray
1053+
.map(a => [Math.random(), a])
1054+
.sort((a, b) => a[0] - b[0])
1055+
.map(a => a[1]);
1056+
1057+
console.log(shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
1058+
// [4, 6, 8, 7, 9, 10, 1, 2, 3, 5]
1059+
```
1060+
1061+
**[⬆ Back to Top](#table-of-contents)**
1062+
### Find max number in array
1063+
1064+
```javascript
1065+
Math.max.apply(null, [1, 2, 10, 3]) // 10
1066+
```
1067+
1068+
**[⬆ Back to Top](#table-of-contents)**
1069+
### Most frequent element in an array
1070+
1071+
```javascript
1072+
const mostFrequent = arr =>
1073+
Object.entries(
1074+
arr.reduce((a, v) => {
1075+
a[v] = a[v] ? a[v] + 1 : 1;
1076+
return a;
1077+
}, {})
1078+
).reduce((a, v) => (v[1] >= a[1] ? v : a), [null, 0])[0];
1079+
1080+
console.log(mostFrequent([1, 2, 4, 2, 5])); // 2
1081+
```

0 commit comments

Comments
 (0)