-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoutput.js
228 lines (171 loc) · 4.38 KB
/
output.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
// 1) What is the output of 10+20+"30" in JavaScript?
//Ans: 3030 because 10+20 will be 30. If there is numeric value before and after +, it treats as binary + (arithmetic operator).
// 2) What is the output of "10"+20+30 in JavaScript?
// Ans: 102030 because after a string all the + will be treated as string concatenation operator (not binary +).
// 3) Output?
// Syncronous
[1, 2, 3, 4].forEach((i) => {
console.log(i);
});
// Asynchronous
function asyncForEach(array, cb) {
array.forEach(() => {
setTimeout(cb, 0);
});
}
asyncForEach([1, 2, 3, 4], (i) => {
console.log(i);
});
// 4) Output? And explain (hint: Octal)
console.log(016);
console.log(017);
console.log(026);
console.log(027);
// 5) Output?
console.log([..."Hello"]);
// OUTPUT
function sum(a, b) {
a = 10;
return [a + b, arguments[0] + arguments[1]];
}
// in "use strict" => [12, 3]
// in normal mood => [12, 12]
console.log(sum(1, 2));
// Event Propagation
// event.currentTaget.tagName
// this.tagName
// sameValuZero algorithm
const sameValueZero = (a, b) => {
if (a === b || (Number.isNaN(a) && Number.isNaN(b))) {
return true;
}
return false;
};
// Output
// ==, ===, object.is, sameValuZero algorithm
const array = [NaN];
const result = array.includes(NaN);
console.log(result); // True: why? => array.includes follows sameValueZero algorithm
// Output
const p = Promise.resolve("Hello");
p.then((val) => {
console.log(val);
return `${val} world`;
}).then((newVal) => {
console.log("new ", newVal);
});
// Output => level => easy
function* counter() {
let index = 0;
while (true) {
yield index++;
}
}
const gen = counter();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
// Output
var y = 1;
if (function f() {}) {
y += typeof f;
}
console.log(y);
// Output
var k = 1;
if (1) {
eval(function foo() {});
k += typeof foo;
}
console.log(k);
// Output
var k = 1;
if (1) {
function foo() {}
k += typeof foo;
}
console.log(k);
// Write a function that would allow you to do this 👉🏻 multiply(5)(6)
// Ans:
function multiply(a) {
return function (b) {
return a * b;
};
}
multiply(5)(6);
// Explain what is callback function is and provide a simple example
function modifyArray(arr, callback) {
arr.push(100);
callback();
}
let arr = [1, 2, 3, 4, 5];
modifyArray(arr, () => {
console.log("Array has been modified ", arr);
});
// Given a string, reverse each word in the sentence
const reverseBySeparator = (str, separator) =>
str.split(separator).reverse().join(separator);
const str = "Welcome to this Javascript Guide!";
const reverseEntireSentence = reverseBySeparator(str, "");
const reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");
console.log(reverseEntireSentence, reverseEachWord);
// Output
function foo(func) {
return func.name;
}
console.log(foo(function myName() {}));
// Output
console.log((1 + 2, 3, 4));
console.log((2, 9 / 3, function () {}));
console.log((3, true ? 2 + 2 : 1 + 1));
// Output
function foo() {
return 1, 2, 3, 4;
}
foo();
// Event Loop output test
function main() {
console.log("A");
setTimeout(function exec() {
console.log("B");
}, 0);
runWhileLoopForNSeconds(3);
console.log("C");
}
main();
function runWhileLoopForNSeconds(sec) {
let start = Date.now(),
now = start;
while (now - start < sec * 1000) {
now = Date.now();
}
}
// Write a function where suppose you want to use a variable for counting something, and you want this counter to be available to all functions.
// Initiate counter
let counter = 0;
// Function to increment counter
function add() {
counter += 1;
}
// Call add() 3 times
add();
add();
add();
// The counter should now be 3
// Solve
const add = (function () {
let counter = 0;
return function () {
counter += 1;
return counter;
};
})();
add();
add();
add(); // the counter is now 3
// Explained
// The variable add is assigned to the return value of a self-invoking function.
// The self-invoking function only runs once. It sets the counter to zero (0), and returns a function expression.
// This way add becomes a function. The "wonderful" part is that it can access the counter in the parent scope.
// This is called a JavaScript closure. It makes it possible for a function to have "private" variables.
// The counter is protected by the scope of the anonymous function, and can only be changed using the add function.