-
Notifications
You must be signed in to change notification settings - Fork 0
/
medium.js
500 lines (415 loc) · 10.7 KB
/
medium.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
Implement a function `calculateTotalSpentByCategory` which takes a list of transactions as parameter
and return a list of objects where each object is unique category-wise and has total price spent as its value.
transactions is an array where each
Transaction - an object like
{
id: 1,
timestamp: 1656076800000,
price: 10,
category: 'Food',
itemName: 'Pizza',
}
Output - [{ category: 'Food', totalSpent: 10 }] // Can have multiple categories, only one example is mentioned here
*/
function calculateTotalSpentByCategory(transactions) {
const res = [];
transactions.forEach(transaction => {
const foundIndex = res.findIndex(item => item.category === transaction.category);
if (foundIndex !== -1) {
res[foundIndex].totalSpent += transaction.price;
} else {
res.push({
category: transaction.category,
totalSpent: transaction.price
});
}
});
return res;
}
module.exports = calculateTotalSpentByCategory;
// const arr = [
// {name:'a',values:[1,2]},
// {name:'b',values:[3]},
// {name:'a',values:[4,5]}
// ]
// const arr2 = [
// {name:'a',values:[1,2,4,5]},
// {name:'b',values:[3]}
// ]
function solve(arr){
const res = []
for(let i = 0 ; i < arr.length; i++){
const present = res.findIndex(item => item.name === arr[i].name);
if (present !== -1) {
res[present].values = [...res[present].values, ...arr[i].values];
} else {
res.push({
name: arr[i].name,
values: [...arr[i].values]
});
}
}
return res;
}
// var originalArray = [{
// id: 1,
// elements: [1, 2]
// },
// {
// id: 1,
// elements: [3, 4]
// },
// {
// id: 5,
// elements: ['a', 'b']
// },
// {
// id: 5,
// elements: ['c', 'd']
// }, {
// id: 27,
// elements: []
// }]
// // I'd like to modify it to look like this (merge by id and join elements):
// newArray = [{
// id: 1,
// elements: [1, 2, 3, 4]
// }, {
// id: 5,
// elements: ['a', 'b', 'c', 'd']
// }, {
// id: 27,
// elements: []
// }]
function solve(arr) {
const res = []
for(let i = 0 ; i < arr.length ; i++){
let find = res.findIndex((item) => item.id === arr[i].id)
if(find != -1){
res[find].elements = [...res[find].elements , ...arr[i].elements];
}else{
res.push({
id : arr[i].id,
elements : arr[i].elements
})
}
}
return res;
}
// the original data array is:
// let data = [
// { "x": 1, "y": 2, "color": "red" },
// { "x": 1, "y": 2, "stroke": "violet" },
// { "x": 3, "y": 4, "color": "green" },
// { "x": 3, "y": 4, "stroke": "blue" }
// ];
// the expected result is:
// let data = [
// { "x": 1, "y": 2, "color": "red", "stroke": "violet" },
// { "x": 3, "y": 4, "color": "green", "stroke": "blue" }
// ];
function solve(obj){
let resultMap = {};
data.forEach(item => {
const key = `${item.x}-${item.y}`;
if (!resultMap[key]) {
resultMap[key] = { x: item.x, y: item.y };
}
Object.assign(resultMap[key], item);
});
let result = Object.values(resultMap);
console.log(result);
}
const myObj = {
name: "John",
age: 30,
cars: [
{name:"Ford", models:["Fiesta", "Focus", "Mustang"]},
{name:"BMW", models:["320", "X3", "X5"]},
{name:"Fiat", models:["500", "Panda"]}
]
}
for(let i in myObj.cars){
x += "<h1>" + myObj.cars[i].name + "</h1>"
for(let j in c){
x += myObj.cars[i].models[j]
}
// IP - "x.y.z", "convert"
// OP - {x: {y: {z: "convert"}}}
function solve(ip) {
const str = ip.split('.');
let res = {};
let temp = res;
for (let i = 0; i < str.length - 1; i++) {
let new_temp = {};
temp[str[i]] = new_temp;
temp = new_temp;
}
temp[str.length - 1] = "convert";
return res;
}
console.log(solve("x.y.z"));
// Identify the mistake in it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class="todo-container">
<h1>My To-do List</h1>
<form onsubmit="(event) => addTodo(event)">
<input type="text" id="todo-input" placeholder="Add a new task" />
<button type="submit">Add</button>
</form>
<ul id="todo-list"></ul>
</div>
<script>
const input = document.getElementById("todo-input");
const todoList = document.getElementById("todo-list");
function addTodo(event) {
event.preventDefault();
const newTodo = input.value.trim();
if (newTodo) {
const li = document.createElement("li");
li.textContent = newTodo;
todoList.addEventListener("click", (e) => {
this.removeChild(e.target);
});
todoList.appendChild(li);
input.value = "";
} else {
console.log("Please eneter a task");
}
}
</script>
</body>
</html>
// Explain event bubbling
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>
<form>
<button>BUTTON</button>
</form>
</div>
<script>
const button = document.querySelector("button");
const form = document.querySelector("form");
const div = document.querySelector("div");
div.addEventListener("click", func);
form.addEventListener("click", func);
button.addEventListener("click", func);
function func(e) {
alert(e.currentTarget.tagName);
}
</script>
</body>
</html>
// Q) What is event capturing?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>
<form>
<button>BUTTON</button>
</form>
</div>
<script>
const button = document.querySelector("button");
const form = document.querySelector("form");
const div = document.querySelector("div");
div.addEventListener("click", func, true);
form.addEventListener("click", func, true);
button.addEventListener("click", func, true);
function func(e) {
alert(e.currentTarget.tagName);
}
</script>
</body>
</html>
// Q) How do you stop propgation?
Wherever you want to stop propogation, add
e.stopPropogation()
// Q) What is event delegation ?
Event delegation is a pattern in JavaScript where instead of attaching an event listener to each individual element,
you attach a single event listener to a parent element. This parent element then
listens for events that bubble up from its descendants. When an event occurs, the parent element's event listener handles it based on the target element that triggered the event.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class="products">
<span class="mobile">mobile</span>
<span class="Headphome">Headphone</span>
<span class="laptop">Laptop</span>
<span class="speaker">Speaker</span>
<span class="USB Dock">USB Dock</span>
<span class="Tripod">Tripod</span>
</div>
<script>
document.querySelector(".products").addEventListener("click", (e) => {
if (e.target.tagName === "SPAN") {
window.location.href += "/" + event.target.className;
}
});
</script>
</body>
</html>
// Flatten the array
const arr = [1, 2, [2, [4, 8, 9], 3], [5, 6]];
function flattenArr(arr) {
const res = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
res.push(...flattenArr(arr[i])); // Recursively flatten nested arrays
} else {
res.push(arr[i]);
}
}
return res;
}
const flattenedArray = flattenArr(arr);
console.log(flattenedArray);
// Flatten the object
const nestedObject = {
a: 1,
b: {
c: 2,
d: {
e: 3,
f: 4,
},
},
g: 5,
};
function flattenObject(obj) {
let res = {};
for (let i = 0; i < obj.length; i++) {
if (typeof obj[i] != Object) {
res[i] = obj[i];
} else {
const flattened = flattenObject(obj[i]);
for (let j in flattened) {
res[i + "." + j] = flattened[j];
}
}
}
return res;
}
//Create a memoise function
function memoise(fn){
const res= {}
return function(...args) {
var argsCache = JSON.stringify(args)
if(!res[argsCache]){
res[argsCache] = fn.call(this,...args)
}else{
return res[argsCache]
}
}
// Implement
// const result = calc.add(10).multiply(10).subtract(0);
// console.log(result);
const calc = {
total: 0,
add(a) {
this.total += a;
return this;
},
multiply(b) {
this.total += b;
return this;
},
subtract(c) {
this.total -= c;
return this;
},
};
//
function outer() {
function inner() {
console.log(x);
}
const x = 5;
return inner;
}
const inner = outer();
inner();
//
Q) What can be the props used to help user work with a reusable library ?
The component like onClick , onChange , onSelect
Q) What is localstorage and session storage ?
Q) What are the container structures used?
Q) What are react.Fragment ?
Q) Differentiate between state and props ? (explain in terms of stateless and props , reusable componenet)
Q) Differentiate useMemo and useCallback ?
Q) What are custom hooks ?
Q) What is redux and how is it used?
Q) What is dffiierence between type and interfaces(Typescript question)?
Q) Give example of where arrow function makes more sense than normal function and vice-versa?
Q) implement
console.log(calculator.lakhs(14).thousand(10).ten(20).result
Q) Implement 3 states of promise
Q) async function print() {
console.log("The promise is pending");
await new Promise(() => {});
}
print();
Q) var obj = {
helloWorld : function(){
return "hello world,, " + this.name;
}
name: 'Hello'
}
var obj2 = {
helloWorld: obj.helloWorld,
name: 'Bye'
}
console.log(obj2.helloWorld)
What does it print?
// Please implement your chunk Break the array into chunk of the size and push it to the resultant array and return it
function chunk(arr,size){}
Q) const sol = [
{
configs: {
type: "object",
properties: {
clientKey: {
type: "string",
title: "Client Id",
},
pspId: {
type: "string",
title: "BillDesk issued merchant id",
minLength: 1,
},
},
},
},
];
const configs: any = sol[0].configs;
const properties = configs.properties;
const formValues: any = {};
for (let key in properties) {
formValues[key] = `<value-entered>`;
}
sol - console.log(formValues);