Skip to content

Commit 20dfde4

Browse files
committed
added array and output questions
1 parent 437caad commit 20dfde4

2 files changed

Lines changed: 88 additions & 32 deletions

File tree

Interview-Questions/arrObj.js

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
let obj = {
77
a: 10,
8-
b: 20
8+
b: 20,
99
};
1010

1111
let obj2 = {
1212
a: 10,
13-
b: 20
13+
b: 20,
1414
};
1515

1616
console.log(obj == obj2);
@@ -69,32 +69,35 @@ let EXAM = [
6969
Subeject: "English",
7070
result: {
7171
point: 80,
72-
grade: "A+"
73-
}
72+
grade: "A+",
73+
},
7474
},
7575
{
7676
Subeject: "Physics",
7777
result: {
7878
point: 85,
79-
grade: "A+"
80-
}
79+
grade: "A+",
80+
},
8181
},
8282
{
8383
Subeject: "Math",
8484
result: {
8585
point: 89,
86-
grade: "A+"
87-
}
88-
}
86+
grade: "A+",
87+
},
88+
},
8989
];
9090

9191
let totalRes = 0;
92-
EXAM.map(subject => (totalRes += subject.result.point));
92+
EXAM.map((subject) => (totalRes += subject.result.point));
9393

9494
console.log(totalRes);
9595
console.log((totalRes / EXAM.length).toFixed(2));
9696

97-
let res = EXAM.reduce((accumulator, currentValue) => accumulator + currentValue.result.point, 0);
97+
let res = EXAM.reduce(
98+
(accumulator, currentValue) => accumulator + currentValue.result.point,
99+
0
100+
);
98101
console.log(res);
99102

100103
/**
@@ -105,70 +108,84 @@ console.log(res);
105108

106109
let obj1 = {
107110
a: 10,
108-
b: 20
111+
b: 20,
109112
};
110113

111114
let obj2 = {
112115
c: 30,
113-
d: 20
116+
d: 20,
114117
};
115118

116119
obj1 = { ...obj1, ...obj2 };
117120
console.log(obj1);
118121

119-
120122
// more test
121123
const a = [1, 2, 5, 7, 9];
122124
const b = [2, 5, 7, 12, 100];
123125

124126
// const c = [...a, ...b];
125127

126-
const c = a.concat(b).sort((a, b) => a > b)
127-
128-
console.log(c)
128+
const c = a.concat(b).sort((a, b) => a > b);
129129

130+
console.log(c);
130131

131132
const obj = {
132133
x: 1,
133134
getX() {
134-
const inner = function() {
135-
console.log(this.x);
136-
}
135+
const inner = function () {
136+
console.log(this.x);
137+
};
137138

138-
inner.bind(this)();
139-
}
140-
}
139+
inner.bind(this)();
140+
},
141+
};
141142

142143
obj.getX();
143144

144-
const arrayTotal = a.reduce((t, i) => t+i);
145-
146-
console.log(arrayTotal)
145+
const arrayTotal = a.reduce((t, i) => t + i);
147146

147+
console.log(arrayTotal);
148148

149149
// OUTPUT
150150
const arr = [1, 2, 3, 4, 5];
151151

152-
arr.push(arr.push(arr.push(arr.pop())))
152+
arr.push(arr.push(arr.push(arr.pop())));
153153

154154
console.log(arr);
155155

156-
157156
// OUTPUT
158157
const arrayOfOddNumbers = [1, 3, 5];
159158
arrayOfOddNumbers[100] = 199;
160159
console.log(arrayOfOddNumbers.length);
161160

162-
163-
164161
// OUTPUT
165162
class MyClass extends (String, Array) {
166163
construct() {}
167164
}
168165

169-
const a = new MyClass()
166+
const a = new MyClass();
170167

171168
console.log(a instanceof Array); // true
172169

173170
// OUTPUT
174-
["1101100000111110","1101110100011111"].map(s => String.fromCharCode(parseInt(s, 2))).reduce((acc, n) => acc + n, "");
171+
["1101100000111110", "1101110100011111"]
172+
.map((s) => String.fromCharCode(parseInt(s, 2)))
173+
.reduce((acc, n) => acc + n, "");
174+
175+
// Preserve immutability of objects
176+
177+
const heroes = [
178+
{ name: "Wolverine", family: "Marvel" },
179+
{ name: "Batman", family: "DC Comics" },
180+
];
181+
182+
const newHeroes = heroes.map((hero) => (hero.name = hero.name.toUpperCase()));
183+
184+
console.log(heroes);
185+
186+
// Right way
187+
const newHeroes2 = heroes.map((hero) =>
188+
Object.assign({}, hero, { name: hero.name.toUpperCase() })
189+
); // or return {...h, name: h.name.toUpperCase()};
190+
191+
console.log(heroes);

Interview-Questions/output.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,42 @@ const reverseEntireSentence = reverseBySeparator(str, "");
146146
const reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");
147147

148148
console.log(reverseEntireSentence, reverseEachWord);
149+
150+
// Output
151+
function foo(func) {
152+
return func.name;
153+
}
154+
155+
console.log(foo(function myName() {}));
156+
157+
// Output
158+
console.log((1 + 2, 3, 4));
159+
160+
console.log((2, 9 / 3, function () {}));
161+
162+
console.log((3, true ? 2 + 2 : 1 + 1));
163+
164+
// Output
165+
function foo() {
166+
return 1, 2, 3, 4;
167+
}
168+
foo();
169+
170+
// Event Loop output test
171+
172+
function main() {
173+
console.log("A");
174+
setTimeout(function exec() {
175+
console.log("B");
176+
}, 0);
177+
runWhileLoopForNSeconds(3);
178+
console.log("C");
179+
}
180+
main();
181+
function runWhileLoopForNSeconds(sec) {
182+
let start = Date.now(),
183+
now = start;
184+
while (now - start < sec * 1000) {
185+
now = Date.now();
186+
}
187+
}

0 commit comments

Comments
 (0)