Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 437caad

Browse files
committed
added coding shortcut, output, function questions and memory garbage collection
1 parent 068740f commit 437caad

File tree

4 files changed

+151
-19
lines changed

4 files changed

+151
-19
lines changed

‎Interview-Questions/func.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
// what is output?
22
const foo = () => {
3-
let a = b = 10;
3+
let a = (b = 10);
44
a++;
5-
return a
6-
}
5+
return a;
6+
};
77

88
foo();
99

1010
console.log(a);
1111
console.log(b);
1212

13-
1413
// output
1514
function createIncrement() {
16-
let count = 0;
15+
let count = 0;
1716

18-
function increment() {
19-
count++;
20-
}
17+
function increment() {
18+
count++;
19+
}
2120

22-
let message = `Count is ${count}`;
23-
function log() {
24-
console.log(message);
25-
}
21+
let message = `Count is ${count}`;
22+
function log() {
23+
console.log(message);
24+
}
2625

27-
return [increment, log];
26+
return [increment, log];
2827
}
2928

3029
const [increment, log] = createIncrement();
@@ -36,22 +35,29 @@ increment();
3635
log();
3736

3837
// Console sequence output?
39-
function run() {
38+
function run() {
4039
const promise = new Promise((resolve) => {
41-
resolve('promise');
42-
})
40+
resolve("promise");
41+
});
4342

4443
setTimeout(() => {
45-
console.log('setTimeout');
44+
console.log("setTimeout");
4645
});
4746

48-
promise.then(res => console.log(res))
47+
promise.then((res) => console.log(res));
4948

50-
console.log('log');
49+
console.log("log");
5150
}
5251

5352
run();
5453

5554
// How does the Javascript interprets the following code?
5655
console.log(x);
5756
var x = 100;
57+
58+
// Output
59+
(function () {
60+
var a = (b = 5);
61+
})();
62+
63+
console.log(b);

‎Interview-Questions/output.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,87 @@ const array = [NaN];
6262

6363
const result = array.includes(NaN);
6464
console.log(result); // True: why? => array.includes follows sameValueZero algorithm
65+
66+
// Output
67+
const p = Promise.resolve("Hello");
68+
p.then((val) => {
69+
console.log(val);
70+
return `${val} world`;
71+
}).then((newVal) => {
72+
console.log("new ", newVal);
73+
});
74+
75+
// Output => level => easy
76+
function* counter() {
77+
let index = 0;
78+
while (true) {
79+
yield index++;
80+
}
81+
}
82+
83+
const gen = counter();
84+
85+
console.log(gen.next().value);
86+
console.log(gen.next().value);
87+
console.log(gen.next().value);
88+
89+
// Output
90+
var y = 1;
91+
if (function f() {}) {
92+
y += typeof f;
93+
}
94+
95+
console.log(y);
96+
97+
// Output
98+
var k = 1;
99+
if (1) {
100+
eval(function foo() {});
101+
k += typeof foo;
102+
}
103+
104+
console.log(k);
105+
106+
// Output
107+
var k = 1;
108+
if (1) {
109+
function foo() {}
110+
k += typeof foo;
111+
}
112+
console.log(k);
113+
114+
// Write a function that would allow you to do this 👉🏻 multiply(5)(6)
115+
116+
// Ans:
117+
function multiply(a) {
118+
return function (b) {
119+
return a * b;
120+
};
121+
}
122+
123+
multiply(5)(6);
124+
125+
// Explain what is callback function is and provide a simple example
126+
127+
function modifyArray(arr, callback) {
128+
arr.push(100);
129+
130+
callback();
131+
}
132+
133+
let arr = [1, 2, 3, 4, 5];
134+
135+
modifyArray(arr, () => {
136+
console.log("Array has been modified ", arr);
137+
});
138+
139+
// Given a string, reverse each word in the sentence
140+
const reverseBySeparator = (str, separator) =>
141+
str.split(separator).reverse().join(separator);
142+
143+
const str = "Welcome to this Javascript Guide!";
144+
const reverseEntireSentence = reverseBySeparator(str, "");
145+
146+
const reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");
147+
148+
console.log(reverseEntireSentence, reverseEachWord);

‎Memory/garbageCollection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Garbage collection algorithms
3+
* - Reference counting algorithm
4+
* - Mark and sweep algorithm
5+
*/
6+
7+
let obj = { a: 10 };
8+
obj = null;
9+
10+
function test() {
11+
let testObj = {};
12+
let testObj2 = {};
13+
14+
// Circular dependency
15+
testObj.key = testObj2;
16+
testObj2.key = testObj;
17+
18+
return "blah";
19+
}
20+
21+
test();

‎js-coding-technique/asyncAwait.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Best Practice
2+
async function getAddress() {
3+
const streetAddress = await getStreetAddress();
4+
const city = await getCity();
5+
const state = await getState();
6+
const zip = await getZip();
7+
8+
return `${streetAddress}, ${city}, ${state}, ${zip}`;
9+
}
10+
11+
// Instated
12+
async function getAddress() {
13+
let [streetAddress, city, state, zip] = await Promise.all([
14+
getStreetAddress(),
15+
getCity(),
16+
getState(),
17+
getZip(),
18+
]);
19+
20+
return `${streetAddress}, ${city}, ${state}, ${zip}`;
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /