-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterviewQuestions.js
More file actions
115 lines (97 loc) · 2.54 KB
/
Copy pathInterviewQuestions.js
File metadata and controls
115 lines (97 loc) · 2.54 KB
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
// Questins 1.
const Students = [
{ name: "ram", age: 24 },
{ name: "rajan", age: 20 },
{ name: "rakesh", age: 22 },
{ name: "Monu", age: 22 },
{ name: "mukesh", age: 20 },
{ name: "Saran", age: 20 },
];
// reduce to an object of age count
const res = Students.reduce((accumulator, current) => {
accumulator[current?.age] = (accumulator[current?.age] || 0) + 1;
return accumulator;
}, {});
// reduce to an object containing names according to the age
const res2 = Students.reduce((accumulator, current) => {
if (!accumulator[current?.age]) {
accumulator[current.age] = [];
}
accumulator[current.age].push(current.name);
return accumulator;
}, {});
console.log("Given Array of Object", Students);
console.log("result=>>>", res);
console.log("result2=>>>", res2);
// =====================================================
// Questions 2
const array = [1, 2, 3, [4, 5, 6, 7, [8, 9, 10], 11], 12];
const ConvertToFlat = (array) => {
let flatArray = [];
for (let i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
flatArray = [...flatArray, ...ConvertToFlat(array[i])];
} else {
flatArray.push(array[i]);
}
}
return flatArray;
};
console.log(
"given Array===>",
array,
"\nFlatten Array=>>",
ConvertToFlat(array)
);
// ===========================================================
// 3. Promise Output based Questions
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
if (true) {
resolve("done !!")
}
reject('failed')
}, 3000)
})
console.log("p1",p1)
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
if (true) {
resolve("done !!")
}
reject('failed')
}, 2000)
})
console.log("p2",p2)
console.log("1. Synchronous log");
setTimeout(() => {
console.log("5. setTimeout with 0ms");
}, 0);
Promise.resolve().then(() => {
console.log("3. First Promise then");
Promise.resolve().then(()=>{
console.log("nested promise")
})
});
const promise= new Promise((resolved,reject)=>{
console.log("inside promise constructor")
resolved(2)
let promise =new Promise((res,rej)=>{
res(4)
})
promise.then(val=>{
console.log("val",val)
})
})
promise.then((value)=>{
console.log("promise fullfill",value);
})
// async function asyncFunc() {
// console.log("2. Inside async function");
// await Promise.resolve().then(()=>{
// console.log("promise inside async function")
// });
// console.log("4. After await inside async function");
// }
// asyncFunc();
console.log("6. After async function call");