-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
412 lines (352 loc) · 8.78 KB
/
index.js
File metadata and controls
412 lines (352 loc) · 8.78 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
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
/* eslint-disable prettier/prettier */
/* eslint-disable no-shadow */
/* eslint-disable no-unused-expressions */
/* eslint-disable prefer-const */
/*
Q1: Given a string, reverse each word in the sentence
Topic: JavaScript
Difficulty: ⭐⭐
For example Welcome to this Javascript Guide! should be become emocleW ot siht tpircsavaJ !ediuG
*/
let str = 'Welcome to this Javascript Guide!';
let reverseStr = str
.split(' ')
.map(e => e.split(''))
.map(item => item.reverse().join(''))
.join(' ');
console.log(reverseStr); // emocleW ot siht tpircsavaJ !ediuG
/*
Q2: Implement enqueue and dequeue using only two stacks
Topic: JavaScript
Difficulty: ⭐⭐
Answer: Enqueue means to add an element, dequeue to remove an element.
*/
let inputStack = []; // First stack
let outputStack = []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
// get the first element that was ever pushed into the input stack
if (stackOutput.length <= 0) {
while (stackInput.length > 0) {
let elementToOutput = stackInput.pop();
stackOutput.push(elementToOutput);
}
}
return stackOutput.pop();
}
/*
Q3: Write a "mul" function which will properly when invoked as below syntax.
Topic: JavaScript
Difficulty: ⭐⭐
console.log(mul(2)(3)(d)); // output : 24
console.log(mul(4)(3)(4)); // output : 48
*/
const mul = a => b => c => a * b * c;
console.log(mul(2)(3)(4)); // output : 24
console.log(mul(4)(3)(4)); // output : 48
/*
Q4: How to empty an array in JavaScript?
Topic: JavaScript
Difficulty: ⭐⭐
var arrayList = ['a', 'b', 'c', 'd', 'e', 'f'];
How could we empty the array above?
*/
let arrayList = ['a', 'b', 'c', 'd', 'e', 'f'];
// method - 1
/*
arrayList = [];
console.log(arrayList);
*/
// method - 2
/*
arrayList.length = 0;
console.log(arrayList);
*/
// method - 3
/*
arrayList.splice(0);
arrayList;
*/
// method - 4
while (arrayList.length) {
arrayList.pop();
}
arrayList;
/*
Q5: How to check if an object is an array or not? Provide some code.
Topic: JavaScript
Difficulty: ⭐⭐
*/
let arrayList1 = [1, 2, 3];
console.log(Array.isArray(arrayList1));
/**
Q6: How would you use a closure to create a private counter?
Topic: JavaScript
Difficulty: ⭐⭐
The closure has access to variable in three scopes:
Variable declared in his own scope
Variable declared in parent function scope
Variable declared in global namespace
*/
function closureCounter() {
let counter = 0;
function increMent(v) {
return (counter += v);
}
function counterValue() {
return counter;
}
return {
increMent,
counterValue,
};
}
const c = closureCounter();
c.increMent(10);
c.increMent(10);
console.log(c.counterValue());
/*
Q7: Write a function that would allow you to do this.
Topic: JavaScript
Difficulty: ⭐⭐
var addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
*/
const createBase = base => value => base + value;
let addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
/*
Q8: How would you check if a number is an integer?
Topic: JavaScript
Difficulty: ⭐⭐
*/
function isInteger(number) {
if (number % 2 === 0 || number % 2 === 1) {
return 'integer';
}
return 'not interger';
}
console.log(isInteger(2));
console.log(isInteger(2.1));
console.log(isInteger(12.3));
console.log(isInteger(0.1));
/*
Q9: Explain what a callback function is and provide a simple example.
Topic: JavaScript
Difficulty: ⭐⭐
*/
const friends = ['Manik', 'Ruhul', 'Habib', 'Fahamid', 'Roti'];
function modifyPrice(arr, cb) {
arr.forEach(i => cb(i))
}
modifyPrice(friends, function (data) {
// your wish what you want with this data in this callback function
console.log(data.toUpperCase());
});
modifyPrice(friends, function (data) {
// your wish what you want with this data in this callback function
console.log(data.toLowerCase());
});
/*
Q10: FizzBuzz Challenge
Topic: JavaScript
Difficulty: ⭐⭐
Create a for loop that iterates up to 100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5.
*/
for (let i = 1; i <= 100; i++) {
let f = i % 3 === 0;
let b = i % 5 === 0;
if (f) {
if (f && b) {
console.log(i, 'FizzBuzz');
} else {
console.log(i, 'Fizz');
}
} else if (b) {
console.log(i, 'Buzz');
}
}
/*
Q11: Make this work
Topic: JavaScript
Difficulty: ⭐⭐
duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]
*/
/*
function duplicate(arr) {
return arr.concat(arr);
}
*/
// or
const arr2 = [1, 2, 3, 4, 5]
function duplicate(arr) {
return [...arr, ...arr]
}
console.log(duplicate(arr2));
/*
Q12: Provide some examples of non-bulean value coercion to a boolean one
Topic: JavaScript
Difficulty: ⭐⭐⭐
*/
// falsy
/*
0,
null,
undefined,
'',
false
*/
// truthy
/*
' ';
[];
{};
function sayHello() {}
*/
/*
Q13: Given two strings, return true if they are anagrams of one another
Topic: JavaScript
Difficulty: ⭐⭐⭐
For example: Mary is an anagram of Army
*/
/*
var firstWord = "manik";
var secondWord = "shakib";
// aikmn ==b= abhiks
*/
// amry ==b= amry
let firstWord = 'Mary';
let secondWord = 'Army';
function anagramsStr(str1, str2) {
let a = str1.toLowerCase();
let b = str2.toLowerCase();
a = a.split('').sort().join('');
b = b.split('').sort().join('');
return a === b;
}
console.log(anagramsStr(firstWord, secondWord)); // true
console.log(anagramsStr('manik', 'shakib')); // false
/*
Q14: What will be the output of the following code?
Topic: JavaScript
Difficulty: ⭐⭐⭐
var y = 1;
if (function f() {}) {
y += typeof f;
}
console.log(y);
*/
// 1undefined
/*
Q15: What will the following code output?
Topic: JavaScript
Difficulty: ⭐⭐⭐
(function() {
var a = b = 5;
})();
console.log(b);
Answer:
The code above will output 5 even though it seems as if the variable was declared within a function and can't be accessed outside of it. This is because
var a = b = 5;
is interpreted the following way:
var a = b;
b = 5;
But b is not declared anywhere in the function with var so it is set equal to 5 in the global scope.
*/
/*
Q16: Write a function that would allow you to do this
Topic: JavaScript
Difficulty: ⭐⭐⭐
multiply(5)(6);
*/
function anotherMultiply(a) {
return function (b) {
return a * b
}
}
console.log(anotherMultiply(10)(5)); // 50
console.log(anotherMultiply(10)(4)); // 40
const multiply = a => b => a * b;
console.log(multiply(10)(5)); // 50
console.log(multiply(10)(4)); // 40
/*
Q17: How does the “this” keyword work? Provide some code examples.
Topic: JavaScript
Difficulty: ⭐⭐⭐⭐
*/
const person = {
name: 'Manik Roy',
email: 'cm.dpi15@gmail.com',
printName() {
return this.name;
}
}
const person2 = {
name: 'CM MANIK Roy',
}
console.log(person.printName.bind(person2)()); // CM MANIK Roy
console.log(person.printName()); // Manik Roy
/*
Q18: Write a recursive function that performs a binary search
Topic: JavaScript
Difficulty: ⭐⭐⭐⭐
*/
/*
Q19: What is “closure” in javascript? Provide an example?
Topic: JavaScript
Difficulty: ⭐⭐⭐⭐
*/
/*
A closure is a function defined inside another function (called parent function) and has access to the variable which is declared and defined in parent function scope.
*/
function closureCounter2() {
let counter = 0;
function increMent(v) {
return (counter += v);
}
function counterValue() {
return counter;
}
return {
increMent,
counterValue,
};
}
/*
Q20: What will be the output of the following code?
Topic: JavaScript
Difficulty: ⭐⭐⭐⭐
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
*/
/*
Answer:
Above code will output 0 as output. delete operator is used to delete a property from an object. Here x is not an object it's local variable. delete operator doesn't affect local variable
*/
/*
Q21: What will be the output of the following code?
Topic: JavaScript
Difficulty: ⭐⭐⭐⭐
var Employee = {
company: 'xyz'
}
var emp1 = Object.create(Employee);
delete emp1.company
console.log(emp1.company);
*/
/*
Above code will output xyz as output. Here emp1 object got company as prototype property. delete operator doesn't delete prototype property.
emp1 object doesn't have company as its own property. You can test it like:
console.log(emp1.hasOwnProperty('company')); //output : false
However, we can delete company property directly from Employee object using delete Employee.company or we can also delete from emp1 object using __proto__ property delete emp1.__proto__.company.
*/