Skip to content

Commit 2f4c225

Browse files
committed
final submit of the 30 days challenges
1 parent 0775cd2 commit 2f4c225

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

CompactObject.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Given an object or array obj, return a compact object.
2+
3+
// A compact object is the same as the original object, except with keys containing falsy values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered falsy when Boolean(value) returns false.
4+
5+
// You may assume the obj is the output of JSON.parse. In other words, it is valid JSON.
6+
7+
8+
var compactObject = function(obj) {
9+
if (Array.isArray(obj)) {
10+
return obj.filter(Boolean).map(compactObject);
11+
}
12+
if (obj === null || typeof obj !== 'object') {
13+
return obj;
14+
}
15+
16+
const result = {};
17+
for (const key in obj) {
18+
const value = compactObject(obj[key]);
19+
if (Boolean(value)) {
20+
result[key] = value;
21+
}
22+
}
23+
return result;
24+
};

EventEmitter.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// 2694. Event Emitter
2+
// Medium
3+
// Companies
4+
// Design an EventEmitter class. This interface is similar (but with some differences) to the one found in Node.js or the Event Target interface of the DOM. The EventEmitter should allow for subscribing to events and emitting them.
5+
6+
// Your EventEmitter class should have the following two methods:
7+
8+
// subscribe - This method takes in two arguments: the name of an event as a string and a callback function. This callback function will later be called when the event is emitted.
9+
// An event should be able to have multiple listeners for the same event. When emitting an event with multiple callbacks, each should be called in the order in which they were subscribed. An array of results should be returned. You can assume no callbacks passed to subscribe are referentially identical.
10+
// The subscribe method should also return an object with an unsubscribe method that enables the user to unsubscribe. When it is called, the callback should be removed from the list of subscriptions and undefined should be returned.
11+
// emit - This method takes in two arguments: the name of an event as a string and an optional array of arguments that will be passed to the callback(s). If there are no callbacks subscribed to the given event, return an empty array. Otherwise, return an array of the results of all callback calls in the order they were subscribed.
12+
13+
class EventEmitter {
14+
events={};
15+
/**
16+
* @param {string} eventName
17+
* @param {Function} callback
18+
* @return {Object}
19+
*/
20+
subscribe(eventName, callback) {
21+
this.events[eventName]=this.events[eventName]||[];
22+
return {
23+
unsubscribe: () => {
24+
this.events[eventName]=this.events[eventName].filter(fn=>fn!==callback)
25+
}
26+
};
27+
}
28+
29+
/**
30+
* @param {string} eventName
31+
* @param {Array} args
32+
* @return {Array}
33+
*/
34+
emit(eventName, args = []) {
35+
const result=[];
36+
if(this.events[eventName]){
37+
for(let fn of this.events[eventName]){
38+
result.push(fn(...args))
39+
}
40+
}
41+
return result;
42+
}
43+
}
44+
45+
/**
46+
* const emitter = new EventEmitter();
47+
*
48+
* // Subscribe to the onClick event with onClickCallback
49+
* function onClickCallback() { return 99 }
50+
* const sub = emitter.subscribe('onClick', onClickCallback);
51+
*
52+
* emitter.emit('onClick'); // [99]
53+
* sub.unsubscribe(); // undefined
54+
* emitter.emit('onClick'); // []
55+
*/

FlattenArrayDepplyNestedArray.js

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

GroupBy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Write code that enhances all arrays such that you can call the array.groupBy(fn) method on any array and it will return a grouped version of the array.
2+
3+
// A grouped array is an object where each key is the output of fn(arr[i]) and each value is an array containing all items in the original array which generate that key.
4+
5+
// The provided callback fn will accept an item in the array and return a string key.
6+
7+
// The order of each value list should be the order the items appear in the array. Any order of keys is acceptable.
8+
9+
// Please solve it without lodash's _.groupBy function
10+
/**
11+
* @param {Function} fn
12+
* @return {Object}
13+
*/
14+
Array.prototype.groupBy = function(fn) {
15+
let l={};
16+
for(let i of this){
17+
l[fn(i)]=[];
18+
19+
}
20+
21+
for(let i of this){
22+
l[fn(i)].push(i);
23+
24+
}
25+
return l;
26+
};
27+
28+
/**
29+
* [1,2,3].groupBy(String) // {"1":[1],"2":[2],"3":[3]}
30+
*/

JoinTwoArrayByID.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Given two arrays arr1 and arr2, return a new array joinedArray. All the objects in each of the two inputs arrays will contain an id field that has an integer value.
2+
3+
// joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length of joinedArray should be the length of unique values of id. The returned array should be sorted in ascending order based on the id key.
4+
5+
// If a given id exists in one array but not the other, the single object with that id should be included in the result array without modification.
6+
7+
// If two objects share an id, their properties should be merged into a single object:
8+
9+
// If a key only exists in one object, that single key-value pair should be included in the object.
10+
// If a key is included in both objects, the value in the object from arr2 should override the value from arr1.
11+
/**
12+
* @param {Array} arr1
13+
* @param {Array} arr2
14+
* @return {Array}
15+
*/
16+
var join = function(arr1, arr2) {
17+
const map = arr1.reduce((acc, item) => {
18+
acc.set(item.id, item);
19+
return acc;
20+
}, new Map());
21+
22+
arr2.forEach(item => {
23+
const existingItem = map.get(item.id) || {};
24+
map.set(item.id, { ...existingItem, ...item });
25+
});
26+
27+
return Array.from(map.values()).sort((a, b) => (a.id < b.id ? -1 : 1));
28+
};

0 commit comments

Comments
 (0)