This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| empty |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Array.prototype.grpBy = function (grpKey) { | |
| const array = this | |
| if (array.length < 1) return {}; | |
| const groupedObj = {}; | |
| array.forEach((item) => { | |
| const propertyKey = item[grpKey]; | |
| if (groupedObj[propertyKey]) { | |
| groupedObj[propertyKey].push(item) | |
| } else { | |
| groupedObj[propertyKey] = [item] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function deepCopy(obj) { | |
| if (obj == null || typeof obj !== 'object') return obj; | |
| if (Array.isArray(obj)) { | |
| return obj.map((item) => deepCopy(item)); | |
| } | |
| let copy = {}; | |
| for (let key in obj) { | |
| if (obj.hasOwnProperty(key)) { | |
| copy[key] = deepCopy(obj[key]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sortBy(arr, fn= ()=>{}) { | |
| const array = [...arr]; | |
| for (let i=0; i<array.length; i++) { | |
| for (let j=i+1;j<array.length;j++){ | |
| if (array[j] < array[i]) { | |
| const temp = array[i]; | |
| array[i] = array[j]; | |
| array[j] = temp; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Definition of Leader: Any Element which is greater than all the values to its right | |
| function findLeadersInArray(arr) { | |
| const leaders = []; | |
| arr.forEach((a, i)=> { | |
| if (i=== arr.length-1) { | |
| leaders.push(a); | |
| return leaders; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function cycle(a, b) { | |
| if (a.length !== b.length) return 0; | |
| const arr = a.split(''); | |
| const temp = [...arr]; // i am copying the array. | |
| let count = 0; | |
| for(let e of arr) { | |
| count += 1; | |
| console.log(temp.join('')); | |
| const firstElement = temp.shift(); // this removes the 1st element of the array | |
| temp.push(firstElement); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // when T is any|unknown, Y is returned, otherwise N | |
| type IsAnyUnknown<T, Y, N> = unknown extends T ? Y : N; | |
| // when T is never, Y is returned, otherwise N | |
| type IsNever<T, Y = true, N = false> = [T] extends [never] ? Y : N; | |
| // when T is a tuple, Y is returned, otherwise N | |
| // valid tuples = [string], [string, boolean], | |
| // invalid tuples = [], string[], (string | number)[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const sum = (a) => (b) => b === undefined ? a : sum(a+b); | |
| const arr = [] | |
| arr[0] = sum(1)(2)(3)(); // output: 6 | |
| arr[1] = sum(1)(2)(3)(1)(1)(2)(2)(10)(); // output: 22 | |
| console.log(arr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function useState(initVal) { | |
| let _val = initVal; | |
| let state = () => _val; | |
| const setState = (newVal) => _val = newVal; | |
| return [state, setState] | |
| } | |
| const [c, setC] = useState(1); | |
| console.log(c()); | |
| setC(2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Array<T> { | |
| customEvery(callback: (each: T, index?: number, array?: Array<T>) => boolean): boolean; | |
| } | |
| Array.prototype.customEvery = function(callback) { | |
| let res= [] | |
| for (let i = 0; i < this.length; i++) res.push(callback(this[i], i, this)); | |
| return res.includes(false) ? false : true | |
| } |
NewerOlder