Skip to content

Commit 7526801

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
add js examples
1 parent 8bf3bcd commit 7526801

11 files changed

+372
-0
lines changed

2666-allow-one-function-call.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,20 @@
1717
var once = function(fn) {
1818
return (...args) => fn && [fn(...args), fn = undefined][0];
1919
};
20+
21+
// var once = function(fn) {
22+
23+
// let hasBeenCalled = false;
24+
// let result;
25+
26+
// return function(...args) {
27+
// if (!hasBeenCalled) {
28+
// result = fn(...args);
29+
// hasBeenCalled = true;
30+
// return result;
31+
// } else {
32+
// return undefined;
33+
// }
34+
// }
35+
36+
// };

2722-join-two-arrays-by-id.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ var join = function(arr1, arr2) {
3030
[...arr1, ...arr2].forEach(obj => map.set(obj.id, { ...map.get(obj.id), ...obj }));
3131
return Array.from(map.values()).sort((a, b) => a.id - b.id);
3232
};
33+
34+
// var join = function(arr1, arr2) {
35+
// const result = {};
36+
// for (let i = 0; i < arr1.length; i++) {
37+
// result[arr1[i].id] = arr1[i];
38+
// }
39+
// for (let i = 0; i < arr2.length; i++) {
40+
// if (result[arr2[i].id]) {
41+
// for (const key in arr2[i]) result[arr2[i].id][key] = arr2[i][key];
42+
// } else {
43+
// result[arr2[i].id] = arr2[i];
44+
// }
45+
// }
46+
47+
// return Object.values(result);
48+
// };

example/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ Better order to solve problems
280280
2634. Filter Elements from Array
281281
2626. Array Reduce Transformation
282282
2629. Function Composition
283+
2666. Allow One Function Call
284+
2623. Memoize
285+
286+
2677. Chunk Array
287+
2631. Group By
288+
2724. Sort By
289+
2722. Join Two Arrays by ID
290+
2625. Flatten Deeply Nested Array
291+
2705. Compact Object
283292

284293
### [Strong List](structy.net)
285294

example/js/2623.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 2623. Memoize
3+
* https://leetcode.com/problems/memoize/
4+
* Difficulty: Medium
5+
*
6+
* Given a function fn, return a memoized version of that function.
7+
*
8+
* A memoized function is a function that will never be called twice
9+
* with the same inputs. Instead it will return a cached value.
10+
*
11+
* You can assume there are 3 possible input functions: sum, fib,
12+
* and factorial.
13+
*
14+
* - `sum` accepts two integers a and b and returns a + b. Assume that
15+
* if a value has already been cached for the arguments (b, a) where
16+
* a != b, it cannot be used for the arguments (a, b). For example,
17+
* if the arguments are (3, 2) and (2, 3), two separate calls should
18+
* be made.
19+
* - `fib` accepts a single integer n and returns 1 if n <= 1 or
20+
* fib(n - 1) + fib(n - 2) otherwise.
21+
* - `factorial` accepts a single integer n and returns 1 if n <= 1 or
22+
* factorial(n - 1) * n otherwise.
23+
*/
24+
25+
/**
26+
* @param {Function} fn
27+
* @return {Function}
28+
*/
29+
function memoize(fn) {
30+
31+
return function(...args) {
32+
33+
}
34+
}
35+
36+
let callCount = 0;
37+
const memoizedFn = memoize(function (a, b) {
38+
callCount += 1;
39+
return a + b;
40+
});
41+
memoizedFn(2, 3); // 5
42+
memoizedFn(2, 3); // 5
43+
console.log(callCount); // 1

example/js/2625.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 2625. Flatten Deeply Nested Array
3+
* https://leetcode.com/problems/flatten-deeply-nested-array/
4+
* Difficulty: Medium
5+
*
6+
* Given a multi-dimensional array arr and a depth n, return a flattened version of that array.
7+
*
8+
* A multi-dimensional array is a recursive data structure that contains integers or other
9+
* multi-dimensional arrays.
10+
*
11+
* A flattened array is a version of that array with some or all of the sub-arrays removed and
12+
* replaced with the actual elements in that sub-array. This flattening operation should only
13+
* be done if the current depth of nesting is less than n. The depth of the elements in the
14+
* first array are considered to be 0.
15+
*
16+
* Please solve it without the built-in Array.flat method.
17+
*/
18+
19+
/**
20+
* @param {Array} arr
21+
* @param {number} depth
22+
* @return {Array}
23+
*/
24+
var flat = function(arr, n) {
25+
26+
};
27+

example/js/2631.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 2631. Group By
3+
* https://leetcode.com/problems/group-by/
4+
* Difficulty: Medium
5+
*
6+
* Write code that enhances all arrays such that you can call the array.groupBy(fn) method on any
7+
* array and it will return a grouped version of the array.
8+
*
9+
* A grouped array is an object where each key is the output of fn(arr[i]) and each value is an
10+
* array containing all items in the original array which generate that key.
11+
*
12+
* The provided callback fn will accept an item in the array and return a string key.
13+
*
14+
* The order of each value list should be the order the items appear in the array. Any order of
15+
* keys is acceptable.
16+
*
17+
* Please solve it without lodash's _.groupBy function.
18+
*/
19+
20+
/**
21+
* @param {Function} fn
22+
* @return {Object}
23+
*/
24+
Array.prototype.groupBy = function(fn) {
25+
26+
};
27+
28+
const array1 = [
29+
{id: 1},
30+
{id: 1},
31+
{id: 2}
32+
]
33+
34+
const fn = (item) => item.id
35+
36+
console.log(array1.groupBy(fn))
37+
// {
38+
// 1: [{id: 1}, {id: 1}],
39+
// 2: [{id: 2}]
40+
// }
41+
42+
// Ex2
43+
const array2 = [1, 2, 3]
44+
console.log(array2.groupBy(String))
45+
// {
46+
// "1": [1],
47+
// "2": [2],
48+
// "3": [3],
49+
// }
50+
51+
// Ex3
52+
const array3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
53+
const fn3 = function (n) {
54+
return String(n > 5);
55+
}
56+
console.log(array3.groupBy(fn3))

example/js/2666.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 2666. Allow One Function Call
3+
* https://leetcode.com/problems/allow-one-function-call/
4+
* Difficulty: Easy
5+
*
6+
* Given a function fn, return a new function that is identical to the original function except
7+
* that it ensures fn is called at most once.
8+
*
9+
* - The first time the returned function is called, it should return the same result as fn.
10+
* - Every subsequent time it is called, it should return undefined.
11+
*/
12+
var once = function(fn) {
13+
14+
return function(...args){
15+
16+
}
17+
};
18+
19+
let fn = (a,b,c) => (a + b + c)
20+
let onceFn = once(fn)
21+
22+
ex1 = onceFn(1,2,3); // 6
23+
ex2 = onceFn(2,3,6); // returns undefined without calling fn
24+
25+
console.log(ex1)
26+
console.log(ex2)
27+

example/js/2677.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 2677. Chunk Array
3+
* https://leetcode.com/problems/chunk-array/
4+
* Difficulty: Easy
5+
*
6+
* Given an array arr and a chunk size size, return a chunked array.
7+
*
8+
* A chunked array contains the original elements in arr, but consists of subarrays each of
9+
* length size. The length of the last subarray may be less than size if arr.length is not
10+
* evenly divisible by size.
11+
*
12+
* You may assume the array is the output of JSON.parse. In other words, it is valid JSON.
13+
*
14+
* Please solve it without using lodash's _.chunk function.
15+
*/
16+
17+
/**
18+
* @param {Array} arr
19+
* @param {number} size
20+
* @return {Array}
21+
*/
22+
var chunk = function(arr, size) {
23+
24+
};
25+
26+
ex1 = chunk([1,2,3,4,5], 1)
27+
console.log(ex1) // [[1],[2],[3],[4],[5]]
28+
29+
ex2 = chunk([1,9,6,3,2], 3)
30+
console.log(ex2) // [[1,9,6],[3,2]]
31+
32+
ex3 = chunk([8,5,3,2,6], 6)
33+
console.log(ex3) // [[8,5,3,2,6]]
34+
35+
ex4 = chunk([], 1)
36+
console.log(ex4) // []
37+

example/js/2705.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 2705. Compact Object
3+
* https://leetcode.com/problems/compact-object/
4+
* Difficulty: Medium
5+
*
6+
* Given an object or array obj, return a compact object.
7+
*
8+
* A compact object is the same as the original object, except with keys containing falsy
9+
* values removed. This operation applies to the object and any nested objects. Arrays are
10+
* considered objects where the indices are keys. A value is considered falsy when
11+
* Boolean(value) returns false.
12+
*
13+
* You may assume the obj is the output of JSON.parse. In other words, it is valid JSON.
14+
*/
15+
16+
/**
17+
* @param {Object|Array} obj
18+
* @return {Object|Array}
19+
*/
20+
var compactObject = function(obj) {
21+
22+
};
23+
24+
ex1 = compactObject([null, 0, false, 1])
25+
console.log(ex1) // [1]
26+
27+
ex2 = compactObject({"a": null, "b": [false, 1]})
28+
console.log(ex2) // {"b": [1]}
29+
30+
ex3 = compactObject([null, 0, 5, [0], [false, 16]])
31+
console.log(ex3) // [5, [], [16]]

example/js/2722.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* 2722. Join Two Arrays by ID
3+
* https://leetcode.com/problems/join-two-arrays-by-id/
4+
* Difficulty: Medium
5+
*
6+
* Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each
7+
* of the two inputs arrays will contain an id field that has an integer value.
8+
*
9+
* joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length
10+
* of joinedArray should be the length of unique values of id. The returned array should be
11+
* sorted in ascending order based on the id key.
12+
*
13+
* If a given id exists in one array but not the other, the single object with that id should
14+
* be included in the result array without modification.
15+
*
16+
* If two objects share an id, their properties should be merged into a single object:
17+
* - If a key only exists in one object, that single key-value pair should be included in
18+
* the object.
19+
* - If a key is included in both objects, the value in the object from arr2 should override
20+
* the value from arr1.
21+
*/
22+
23+
/**
24+
* @param {Array} arr1
25+
* @param {Array} arr2
26+
* @return {Array}
27+
*/
28+
var join = function(arr1, arr2) {
29+
30+
};
31+
32+
33+
arr1 = [
34+
{"id": 1, "x": 1},
35+
{"id": 2, "x": 9}
36+
],
37+
arr2 = [
38+
{"id": 3, "x": 5}
39+
]
40+
41+
ex1 = join(arr1, arr2)
42+
console.log(ex1)
43+
// [
44+
// {"id": 1, "x": 1},
45+
// {"id": 2, "x": 9},
46+
// {"id": 3, "x": 5}
47+
// ]
48+
49+
50+
arr1 = [
51+
{"id": 1, "x": 2, "y": 3},
52+
{"id": 2, "x": 3, "y": 6}
53+
],
54+
arr2 = [
55+
{"id": 2, "x": 10, "y": 20},
56+
{"id": 3, "x": 0, "y": 0}
57+
]
58+
59+
ex2 = join(arr1, arr2)
60+
console.log(ex2)
61+
// [
62+
// {"id": 1, "x": 2, "y": 3},
63+
// {"id": 2, "x": 10, "y": 20},
64+
// {"id": 3, "x": 0, "y": 0}
65+
// ]
66+
67+
arr1 = [
68+
{"id": 1, "b": {"b": 94}, "v": [4, 3], "y": 48}
69+
],
70+
arr2 = [
71+
{"id": 1, "b": {"c": 84}, "v": [1, 3]}
72+
]
73+
74+
ex3 = join(arr1, arr2)
75+
console.log(ex3)
76+
// [
77+
// {"id": 1, "b": {"c": 84}, "v": [1, 3], "y": 48}
78+
// ]

0 commit comments

Comments
 (0)