-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsinterviewpratice.js
384 lines (266 loc) · 8.22 KB
/
jsinterviewpratice.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
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
//////////////////////
const profile = {
name:'Peter',
age: 56,
kids: [{
name: 'jil',
age: 23,
kids: [{
name: 'jeter',
age: 1
},
{
name: 'bill',
age: 2
}
]
}]
}
//document.write(<div style ="color:white"></div>);
//in javascript object are not itebrale unless they do not use Itebrale protocal. There we can not use for of loop on object
console.log('Use of Recursion');
let totalAge = 0;
totalAge = totalAge + profile.age;
const addAge = function (obj){
if(Array.isArray(obj.kids)){
obj.kids.forEach((elem)=>{
totalAge = totalAge + elem.age;
addAge(elem);
})
return totalAge;
}
}
console.log(addAge(profile));
console.log('Use of Recursion end');
////////////////////////
console.log('currying=>recursion start');
//add(3).mul(5).divide(3) output should be 5
const add = function(a){
return {
mul: function (b){
let mul = a*b;
return {
divide: function (c){
return mul/c;
}
}
}
}
}
//const output = add(3);
console.log(add(3).mul(5).divide(3));
console.log('currying=>recursion end');
console.log('for in loop demo');
const forInLoopObj = {
name:'Peter',
age: 56,
kids: [{
name: 'jil',
age: 23,
kids: [{
name: 'jeter',
age: 1
},
{
name: 'bill',
age: 2
}
]
}]
}
function dump_properties(obj, obj_name){
let result = '';
for(let i in obj){
result += obj_name+'.'+ i + '=' + obj[i] + '</br/>';
}
return result;
}
console.log(dump_properties(forInLoopObj, 'Member'));
console.log('for in loop demo end');
console.log('difference between for in and for of loop ');
const arr = [3, 5, 7, 9, 'rakesh'];
arr.foo = 'hello';
//console.dir(arr);
for(let i in arr){
console.log('using for in i='+ i);
}
for(let i of arr){
console.log('using for of i='+ i);
}
console.log('difference between for in and for of loop end');
console.log('remove duplicate from array in multiple ways start');
//remove duplicate from array method1
const duplicateArr1 = [5, 2, 3, 5, 2, 5, 2, 5, 2, 5, 4];
const arrSet = new Set(duplicateArr1);
console.log(arrSet);
console.log('remove duplicate using set is', [...arrSet]);
console.log('remove duplicate from array second way start');
//remove duplicate from array method2
const duplicateArr2 = [5, 2, 3, 5, 2, 5, 2, 5, 2, 5, 4];
function removeDupicate2(arr){
const outputArr = [];
for(let val of arr){
if(outputArr.indexOf(val) === -1){
outputArr.push(val);
}
}
return outputArr;
}
console.log('remove duplicate2 is ', removeDupicate2(duplicateArr2));
console.log('remove duplicate from array second way end');
////remove duplicate from array method3 valila without builtin
console.log('remove duplicate from array 3rd way start');
const duplicateArr3 = [5, 2, 3, 5, 2, 5, 2, 5, 2, 5, 4];
function searchInArr(arr, val){
var hasFound = false;
if(arr.length === 0){
hasFound = false;
return hasFound;
}
for(let i=0; i< arr.length; i++){
if(arr[i] === val){
hasFound = true;
return hasFound;
}
}
}
function removeDuplicate3rd (arr){
const outputArr = [];
for(let i=0; i< arr.length; i++){
let val = arr[i];
let res = searchInArr(outputArr, val);
if(!res){
outputArr.push(val);
}
}
return outputArr;
}
console.log('removeDuplicate3rd is ', removeDuplicate3rd(duplicateArr3));
console.log('remove duplicate from array 3rd way end');
console.log('remove duplicate from array 4th Way way start');
//remove duplicate 4th way
// 1 issue with this is that order will not maintain
function removeDuplicate4(){
const duplicateArr4 = [5, 2, 3, 5, 2, 5, 2, 5, 2, 5, 4];
const objdup = {};
const outPutArrDup = [];
duplicateArr4.forEach((val, indx, arr)=>{
objdup[val] = true;
})
outPutArrDup.push(...Object.keys(objdup));
return outPutArrDup;
}
console.log('removeduplicate4thwat', removeDuplicate4());
console.log('remove duplicate from array 4th Way way end');
console.log('remove duplicate from array 5th Way way start');
//remove duplicate 5th way
function removeDuplicate5(){
const duplicateArr5 = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl", "Mike", "Nancy"];
return duplicateArr5.filter((val, indx, arr)=>{
return arr.indexOf(val) === indx;
})
}
console.log('removeduplicate5thhway', removeDuplicate5());
console.log('remove duplicate from array 5th Way way end');
console.log('remove duplicate from array in multiple ways end');
////////////// private constructor/singleton in javascript
//Object.seal() //prevent adding and deleting properteies from object
//Whearesa Object.freeze does same as seal but can not modify/update as well
console.log('singolton in javascript start method1');
class User {
firstName= "";
lastName= "";
constructor(fName, lName){
if(User.instance instanceof User){
return User.instance;
}
this.firstName = fName;
this.lastName = lName;
Object.freeze(this);
User.instance = this;
}
calculateAge= function(){
return this.firstName + ' '+ this.lastName;
}
}
const obj1 = new User('Suresh', 'Gupta');
const obj2 = new User('Rakesh', 'Yadav');
const obj3 = new User('Tejesh', 'Roy');
console.log('fname=', obj1.firstName);
console.log('user obj1 is=', obj1);
console.log('user obj2 is=', obj2);
console.log('user obj3 is=', obj3);
obj2.firstName ='Kapil';
console.log('obj2', obj2);
console.log('singolton in javascript method1 end');
////////////// private constructor/singleton in javascript end
/////////////////////// programs start ///////////////////////////
/******************** Plaindrome Start */
console.log('palindrome start');
const palindromFun = (function (){
function checkPalinDrome(testStr){
let tempStr = testStr.toString().split('').reverse().join('');
if(testStr.toString() === tempStr ){
return true;
}
return false;
}
return {
palindrom1: checkPalinDrome
}
})();
if(palindromFun.palindrom1(12321)){
console.log('its palindrome');
}else{
console.log('its not palindrome');
}
//////////////////// methot 2 start /////////////////////
const palindromFun2 = (function (){
function checkPalinDrome(testInput){
let tempStr = testInput.toString().split('');
let newArr = [];
for(let i= tempStr.length-1; i>=0; i--){
newArr.push(tempStr[i]);
}
let newAtr = newArr.join('');
if(testInput.toString() === newAtr){
return true;
}
return;
}
return {
palindrom1: checkPalinDrome
}
})();
if(palindromFun2.palindrom1('madam')){
console.log('Cheking using methd 2 . its palindrome');
}else{
console.log('Cheking using methd 2 . its not palindrome');
}
///////////////////// method 2 end ////////////////////
console.log('palindrome end');
/******************** Plaindrome end */
///////////////// Fibonacci series start ////////////////////
// fibonacci is a number is sum of previous 2 numbers e.g 0 1 1 2 3 5 8 13 21 34 55 89
console.log('fibnocci method 1 start');
function fibonacci1(n){
let arr = [0, 1];
for(let i=2; i< n+1; i++){
arr.push(arr[i-2] + arr[i-1]);
}
return arr[n];
}
console.log('fibnocci series', fibonacci1(11));
console.log('fibnocci method1 end');
console.log('fibnocci method 2 start');
function fibonacci2(n){
if(n < 2){
return n;
}
// 0 1 1 2 3 5 8 13 21 34 55 89
return fibonacci2(n-2) + fibonacci2(n-1);
}
console.log('fibnocci series methot 2', fibonacci2(11));
console.log('fibnocci method2 end');
///////////////// Fibonacci series end ////////////////////
////////////////////////////// programs end ////////////////////