|
11 | 11 | |3 | [Convert truthy/falsy to boolean(true/false)](#Convert-truthy-falsy-to-boolean)|
|
12 | 12 | |4 | [Repeat a string](#Repeat-a-string)|
|
13 | 13 | |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)| |
15 | 15 | |7 | [Did you know you can flat an array?](#Did-you-know-you-can-flat-an-array)|
|
16 | 16 | |8 | [Get unique values in an array](#Get-unique-values-in-an-array)|
|
17 | 17 | |9 | [Copy Text to Clipboard](#Copy-Text-to-Clipboard)|
|
|
54 | 54 | |46 | [get device orientation](#get-device-orientation)|
|
55 | 55 | |47 | [CONST vs LET vs VAR](#const-let-var)|
|
56 | 56 | |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)| |
57 | 60 |
|
58 | 61 | **[⬆ Back to Top](#table-of-contents)**
|
59 | 62 | ### How to generate a random number in a given range
|
@@ -162,12 +165,12 @@ console.log("this doSomething took " + (endTime - startTime) + " milliseconds.")
|
162 | 165 | ```
|
163 | 166 |
|
164 | 167 | **[⬆ 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 |
166 | 169 |
|
167 | 170 | ```javascript
|
168 | 171 | // Mutating way
|
169 | 172 | const muatatedArray = ['a','b','c','d','e'];
|
170 |
| -muatatedArray.splice(2,1) |
| 173 | +muatatedArray.splice(2, 1) |
171 | 174 | console.log(muatatedArray) // ['a', 'b', 'd', 'e']
|
172 | 175 |
|
173 | 176 | // Non-mutating way
|
@@ -200,7 +203,7 @@ myArray.flat(infinity) // [2, 3, 4, 5, 7, 7, 8, 9, 1, 1];
|
200 | 203 | const numbers = [1, 1, 3, 2, 5, 3, 4, 7, 7, 7, 8];
|
201 | 204 |
|
202 | 205 | // 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 ) |
204 | 207 | console.log(unieqNumbers) // [1, 3, 2, 5, 4, 7, 8]
|
205 | 208 |
|
206 | 209 | // Ex2
|
@@ -789,27 +792,26 @@ const reduceRightArray = arr.reduceRight((acc, current) => {
|
789 | 792 | **[⬆ Back to Top](#table-of-contents)**
|
790 | 793 | ### Abort Fetch
|
791 | 794 |
|
792 |
| -```javascript |
793 |
| -
|
794 |
| -
|
795 |
| -// HTML |
| 795 | +```html |
| 796 | +<!-- HTML --> |
796 | 797 | <button id="download">Download</button>
|
797 | 798 | <button id="abort">Abort</button>
|
798 | 799 |
|
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> |
813 | 815 | ```
|
814 | 816 |
|
815 | 817 |
|
@@ -1010,7 +1012,7 @@ function getOrientation() {
|
1010 | 1012 | **[⬆ Back to Top](#table-of-contents)**
|
1011 | 1013 | ### slice array with steps (like python)
|
1012 | 1014 |
|
1013 |
| -```js |
| 1015 | +```javascript |
1014 | 1016 |
|
1015 | 1017 | Array.prototype.slice = function (start = 0, end = this.length, step = 1) {
|
1016 | 1018 | const result = [];
|
@@ -1040,3 +1042,40 @@ console.log(array.slice(1, 9, 2)); // [1, 3, 5, 7]
|
1040 | 1042 | console.log(array.slice(1, 9, -2)); // [7, 5, 3, 1]
|
1041 | 1043 |
|
1042 | 1044 | ```
|
| 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