Skip to content

Commit 2b14da1

Browse files
authored
readme changes (codesONLY#93)
* 🎨 update and clear the clutter * 📝 improve README.md * feat: add prototype, promises code snippets * feat: add prototype, promises code snippets * feat: add functions, objects, recursion etc. snippets * feat: add more snippets in objects, closures and arrays * feat: add functions code * feat: add promise pollyfils * feat: add object and functions snippets * feat: add array questions * feat: add calculator chaining code * feat: add memoize one * add more solutions snippets
1 parent cc4a76a commit 2b14da1

89 files changed

Lines changed: 2123 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Snippets/Arrays/arraySubset.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const arr1 = [2, 3, 4, 5, 6, 88, 9];
2+
const arr2 = [3, 3, 3, 5, 100];
3+
4+
function isSubset(arr1, arr2) {
5+
const arr1Set = [];
6+
const arr2Set = [];
7+
8+
arr1.forEach((item) => {
9+
if (!arr1Set.includes(item)) {
10+
arr1Set.push(item);
11+
}
12+
});
13+
arr2.forEach((item) => {
14+
if (!arr2Set.includes(item)) {
15+
arr2Set.push(item);
16+
}
17+
});
18+
19+
const res = arr1Set.reduce((previousValue, currentValue) => {
20+
return previousValue + +arr2Set.includes(currentValue);
21+
}, 0);
22+
return res === arr2Set.length;
23+
}
24+
25+
console.log(isSubset(arr1, arr2));

Snippets/Arrays/atPolyfill.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Array.prototype.myAt = myAt;
2+
3+
function myAt(index) {
4+
if (index >= 0) {
5+
return this[index];
6+
}
7+
return this[this.length - -index];
8+
}
9+
10+
console.log([1, 2, 3].myAt(-4));

Snippets/Arrays/filterPolyfill.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Array.prototype.myFilter = myFilter;
2+
3+
function myFilter(cb) {
4+
const filteredArr = [];
5+
this.forEach((item) => {
6+
if (cb(item)) {
7+
filteredArr.push(item);
8+
}
9+
});
10+
return filteredArr;
11+
}
12+
13+
const arr = [1, 2, 3, 4];
14+
console.log(arr.myFilter((item) => item !== 2));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function flattenArray(arr, n = Infinity) {
2+
let res = [];
3+
for (i in arr) {
4+
if (n !== 0 && Array.isArray(arr[i])) {
5+
res.push(...flattenArray(arr[i], n - 1));
6+
} else {
7+
res.push(arr[i]);
8+
}
9+
}
10+
return res;
11+
}
12+
let input = [
13+
1,
14+
2,
15+
3,
16+
[4],
17+
[5, 6, [7], [8, [9, [10]]]],
18+
11,
19+
12,
20+
13,
21+
[14, [[[[[15, [16]]]]]]],
22+
17,
23+
18,
24+
[19, [20, [21, [22, [23, [24, [[[[[25]]]]]]]]]]],
25+
];
26+
27+
console.log(flattenArray(input, 2));

Snippets/Arrays/groupBy.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
const arr = [6.1, 2.4, 2.7, 6.8];
2+
3+
function groupBy(arr, property) {
4+
const groupByString = (arr, query) => {
5+
let res = {};
6+
7+
arr.forEach((item, index) => {
8+
const queryList = query.split(".");
9+
10+
let currKey;
11+
let currItem = item;
12+
let count = 0;
13+
for (let i = 0; i < queryList.length; i++) {
14+
currKey = queryList[i];
15+
if (!currItem[currKey]) {
16+
break;
17+
} else {
18+
count += 1;
19+
currItem = currItem[currKey];
20+
}
21+
}
22+
23+
if (count !== queryList.length) {
24+
currItem = undefined;
25+
}
26+
if (!res[currItem]) {
27+
res[currItem] = [item];
28+
} else {
29+
res[currItem].push(item);
30+
}
31+
});
32+
return res;
33+
};
34+
const groupByCallback = (arr, cb) => {
35+
let res = {};
36+
arr.forEach((item) => {
37+
const ans = cb.call(null, item);
38+
39+
if (Object.keys(res).includes(ans.toString())) {
40+
res[ans].push(item);
41+
} else {
42+
res[ans] = [item];
43+
}
44+
});
45+
return res;
46+
};
47+
48+
let res;
49+
switch (typeof property) {
50+
case "function":
51+
res = groupByCallback(arr, property);
52+
break;
53+
case "string":
54+
res = groupByString(arr, property);
55+
break;
56+
default:
57+
return new Error("Invalid Property Type");
58+
}
59+
60+
return res;
61+
}
62+
63+
// groupBy(arr, Math.floor);
64+
console.log(groupBy(arr, Math.ceil));
65+
console.log(groupBy([6.1, 4.2, 6.3], Math.floor));
66+
console.log(groupBy(["one", "two", "three"], "length"));
67+
console.log(
68+
groupBy(
69+
[{ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }, { a: { b: { c: 2 } } }],
70+
"b.a.c"
71+
)
72+
);
73+
74+
console.log(
75+
groupBy(
76+
[{ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } }, { a: { b: { c: 2 } } }],
77+
"a.b.c"
78+
)
79+
);
80+
81+
// alternate solution
82+
function groupByAlternative(collection, property) {
83+
const output = {};
84+
85+
if (!collection || typeof collection !== "object") {
86+
return output;
87+
}
88+
89+
const isPropertyFunction = typeof property === "function";
90+
const isPropertyPath = typeof property === "string";
91+
92+
for (const value of Object.values(collection)) {
93+
let current = undefined;
94+
95+
if (isPropertyFunction) {
96+
current = property(value);
97+
} else if (isPropertyPath) {
98+
// a.b.c -> [a, b, c];
99+
const path = property.split(".");
100+
let i;
101+
let currentKey;
102+
let currentItem = value; // { a: { b: { c: 1 } } }
103+
104+
for (i = 0; i < path.length; i++) {
105+
// [a, b, c] -> currentKey: path[0] -> a
106+
// [a, b, c] -> currentKey: path[1] -> b
107+
// [a, b, c] -> currentKey: path[2] -> c
108+
currentKey = path[i];
109+
110+
if (!currentItem[currentKey]) {
111+
currentItem = undefined;
112+
break;
113+
}
114+
currentItem = currentItem[currentKey];
115+
}
116+
117+
current = currentItem;
118+
}
119+
120+
output[current] = output[current] || [];
121+
output[current].push(value);
122+
}
123+
124+
return output;
125+
}

Snippets/Arrays/mapPolyfill.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Array.prototype.myMap = myMap;
2+
3+
function myMap(cb) {
4+
const newArr = [];
5+
for (let i = 0; i < this.length; i++) {
6+
newArr.push(cb(this[i]));
7+
}
8+
return newArr;
9+
}
10+
const arr = [1, 2, 3, 4, 5];
11+
12+
console.log(arr.myMap((item) => item + 2));

Snippets/Arrays/pushPolyfill.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Array.prototype.myPush = myPush;
2+
3+
function myPush(item) {
4+
this[this.length] = item;
5+
return this;
6+
}
7+
const arr = [1, 2, 3];
8+
arr.myPush(2).push(2); // chaining possible
9+
console.log(arr);

Snippets/Arrays/reducePolyfill.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Array.prototype.myReduce = myReduce;
2+
3+
function myReduce(cb, initialValue = this[0]) {
4+
let result = initialValue;
5+
this.forEach((item) => {
6+
result = cb(result, item);
7+
});
8+
return result;
9+
}
10+
11+
const array1 = [1, 2, 3, 4];
12+
const sumWithInitial = array1.myReduce(
13+
(previousValue, currentValue) => previousValue + currentValue,
14+
0
15+
);
16+
17+
console.log(sumWithInitial);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const arr = [1, 2, 2, 3];
2+
3+
const updatedArrWithSet = [...new Set(arr)];
4+
5+
const updatedArrWithFilter = arr.filter((c, index) => {
6+
return index !== arr.indexOf(c);
7+
});
8+
9+
const updatedArrWithIncludes = [];
10+
arr.forEach((c) => {
11+
if (!updatedArrWithIncludes.includes(c)) {
12+
updatedArrWithIncludes.push(c);
13+
}
14+
});
15+
16+
console.log(updatedArrWithIncludes);

Snippets/Arrays/unshiftPolyfill.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Array.prototype.myUnshift = myUnshift;
2+
3+
function myUnshift(item) {
4+
let prevItem = item;
5+
const originalLength = this.length;
6+
for (let i = 0; i <= originalLength; i++) {
7+
let temp = this[i];
8+
this[i] = prevItem;
9+
prevItem = temp;
10+
}
11+
return this;
12+
}
13+
14+
const arr = [1, 2, 3, 4];
15+
arr
16+
.myUnshift(2)
17+
.myUnshift(100)
18+
.myUnshift(() => console.log("WOW"));
19+
console.log(arr);

0 commit comments

Comments
 (0)