forked from frankxuzy/JS-TheTrickyStuff
/
Test.js
1029 lines (837 loc) · 32.2 KB
/
Test.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
setTimeout(function(){
var counter = 0;
counter++;
console.log("Counter:", counter);
setTimeout(function(){
counter++;
console.log("Counter:", counter);
setTimeout(function(){
counter++;
console.log("Counter:", counter);
}, 3000);
}, 2000);
}, 1000);
// refactor using promise
// step 1
var counter = 0;
var incCounter = () => {
counter++;
console.log(`Counter: ${counter}`);
}
// step 2
var runLater = (callback, runInMs) => {
var p = new Promise((resolve, reject) => {
setTimeout(() => {
var res = callback();
resolve(res);
}, runInMs);
});
return p;
}
// step 3 Promise Chain
runLater(incCounter, 1000).then(() => {
return runLater(incCounter, 2000);
}).then(() => {
return runLater(incCounter, 3000);
}).then(() => {});
// forEach exercise ===================================
/*
Write a function called doubleValues which accepts an array and returns a new array with all the values in the array passed to the function doubled
Examples:
doubleValues([1,2,3]) // [2,4,6]
doubleValues([5,1,2,3,10]) // [10,2,4,9,20]
*/
function doubleValues(arr){
var newArr = [];
arr.forEach(function(val){
newArr.push(val*2);
})
return newArr;
}
/*
Write a function called onlyEvenValues which accepts an array and returns a new array with only the even values in the array passed to the function
Examples:
onlyEvenValues([1,2,3]) // [2]
onlyEvenValues([5,1,2,3,10]) // [2,10]
*/
function onlyEvenValues(arr){
var newArr = [];
arr.forEach(function(val){
if(val % 2 === 0) {
newArr.push(val);
}
});
return newArr;
}
/*
Write a function called showFirstAndLast which accepts an array of strings and returns a new array with only the first and last character of each string.
Examples:
showFirstAndLast(['colt','matt', 'tim', 'udemy']) // ["ct", "mt", "tm", "uy"]
showFirstAndLast(['hi', 'goodbye', 'smile']) // ['hi', 'ge', 'se']
*/
function showFirstAndLast(arr){
var newArr = [];
arr.forEach(function(val){
// batter solution
// newArr.push(val[0] + val[val.length-1])
var firstLastStr = val.substr(0,1) + val.substring(val.length - 1);
newArr.push(firstLastStr);
});
return newArr;
}
/*
Write a function called addKeyAndValue which accepts an array, a key, and a value and returns a the array passed to the function with the new key and value added for each variable
Examples:
addKeyAndValue([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'title', 'instructor')
// [{name: 'Elie', title:'instructor'}, {name: 'Tim', title:'instructor'}, {name: 'Matt', title:'instructor'}, {name: 'Colt', title:'instructor'}]
*/
function addKeyAndValue(arr,key,value){
var newArr = [];
arr.forEach(function(val){
val[key] = value;
newArr.push(val);
})
return newArr;
}
// function addKeyAndValue(arr,key,value){
// arr.forEach(function(val){
// val[key] = value;
// });
// return arr;
// }
/*
Write a function called vowelCount which accepts a string and returns an object with the keys as the vowel and the values as the number of times the vowel appears in the string. This function should be case insensitive so a lowercase letter and uppercase letter should count
Examples:
vowelCount('Elie') // {e:2,i:1};
vowelCount('Tim') // {i:1};
vowelCount('Matt') // {a:1})
vowelCount('hmmm') // {};
vowelCount('I Am awesome and so are you') // {i: 1, a: 4, e: 3, o: 3, u: 1};
*/
function vowelCount(str){
var lowStr = str.toLowerCase();
var a = 0, e = 0, i = 0, o = 0, u = 0;
var vowelObj = {};
lowStr.split("").forEach(function(val){
switch(val) {
case "a":
a += 1;
vowelObj['a'] = a;
break;
case "e":
e += 1;
vowelObj['e'] = e;
break;
case "i":
i += 1;
vowelObj['i'] = i;
break;
case "o":
o += 1;
vowelObj['o'] = o;
break;
case "u":
u += 1;
vowelObj['u'] = u;
break;
}
});
return vowelObj;
}
// batter solution
function vowelCount(str){
var splitArr = str.toLowerCase().split("");
var obj = {};
var vowels = "aeiou";
splitArr.forEach(function(letter){
// if(vowels.indexOf(letter) !== -1){
if(vowels.includes(letter)){
if(obj[letter]){
obj[letter]++;
} else{
obj[letter] = 1;
}
}
});
return obj;
}
console.log(vowelCount('I Am awesome and so are you'));
// map exercise ===================================
/*
Write a function called doubleValues which accepts an array and returns a new array with all the values in the array passed to the function doubled
Examples:
doubleValues([1,2,3]) // [2,4,6]
doubleValues([1,-2,-3]) // [2,-4,-6]
*/
function doubleValues(arr){
return arr.map(function(val) {
return val * 2;
});
}
/*
Write a function called valTimesIndex which accepts an array and returns a new array with each value multiplied by the index it is currently at in the array.
Examples:
valTimesIndex([1,2,3]) // [0,2,6]
valTimesIndex([1,-2,-3]) // [0,-2,-6]
*/
function valTimesIndex(arr){
return arr.map(function(val, i){
return val * i;
})
}
/*
Write a function called extractKey which accepts an array of objects and some key and returns a new array with the value of that key in each object.
Examples:
extractKey([{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}], 'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractKey(arr, key){
return arr.map(function(val) {
return val[key];
});
}
/*
Write a function called extractFullName which accepts an array of objects and returns a new array with the value of the key with a name of "first" and the value of a key with the name of "last" in each object, concatenated together with a space.
Examples:
extractFullName([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia"}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele"}]) // ['Elie Schoppik', 'Tim Garcia', 'Matt Lane', 'Colt Steele']
*/
function extractFullName(arr){
return arr.map(function(val){
return val['first'] + ' ' + val['last'];
})
}
// filter exercise ===================================
/*
Write a function called filterByValue which accepts an array of objects and a key and returns a new array with all the objects that contain that key.
Examples:
filterByValue([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner') // [{first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Colt', last:"Steele", isCatOwner: true}]
*/
function filterByValue(arr, key){
return arr.filter(function(val){
return val[key];
});
}
/*
Write a function called find which accepts an array and a value and returns the first element in the array that has the same value as the second parameter or undefined if the value is not found in the array.
Examples:
find([1,2,3,4,5], 3) // 3
find([1,2,3,4,5], 10) // undefined
*/
function find(arr, searchValue){
return arr.filter(function(val){
return val === searchValue;
})[0];
}
/*
Write a function called findInObj which accepts an array of objects, a key, and some value to search for and returns the first found value in the arrayt.
Examples:
findInObj([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner',true) // {first: 'Tim', last:"Garcia", isCatOwner: true}
*/
function findInObj(arr, key, searchValue){
return arr.filter(function(val, i){
console.log(i);
return val[key] === searchValue;
})[0];
}
// high efficiency
function findInObj(arr, key, searchValue){
for(var i = 0; i < arr.length; i++)
{
if(arr[i][key] === searchValue){
console.log(i);
return arr[i];
}
}
}
/*
Write a function called removeVowels which accepts a string and returns a new string with all of the vowels (both uppercased and lowercased) removed. Every character in the new string should be lowercased.
Examples:
removeVowels('Elie') // ('l')
removeVowels('TIM') // ('tm')
removeVowels('ZZZZZZ') // ('zzzzzz')
*/
function removeVowels(str){
var lowerCaseArr = str.toLowerCase().split('');
var vowels = 'aeiou';
return lowerCaseArr.filter(function(val){
return !vowels.includes(val)
}).join('');
}
/*
Write a function called doubleOddNumbers which accepts an array and returns a new array with all of the odd numbers doubled (HINT - you can use map and fitler to double and then filter the odd numbers).
Examples:
doubleOddNumbers([1,2,3,4,5]) // [2,6,10]
doubleOddNumbers([4,4,4,4,4]) // []
*/
function doubleOddNumbers(arr){
var oddArr = arr.filter(function(val){
return val % 2 !== 0;
});
return oddArr.map(function(val){
return val * 2
});
}
// some and every exercise =============================
/*
Write a function called hasOddNumber which accepts an array and returns true if the array contains at least one odd number, otherwise it returns false.
Examples:
hasOddNumber([1,2,2,2,2,2,4]) // true
hasOddNumber([2,2,2,2,2,4]) // false
*/
function hasOddNumber(arr){
return arr.some(function(val){
return val % 2 !== 0;
});
}
/*
Write a function called hasAZero which accepts a number and returns true if that number contains at least one zero. Otherwise, the function should return false
Examples:
hasAZero(3332123213101232321) // true
hasAZero(1212121) // false
*/
function hasAZero(arr){
return arr.toString().split('').some(function(val){
return parseInt(val) === 0;
});
}
/*
Write a function called hasOnlyOddNumbers which accepts an array and returns true if every single number in the array is odd. If any of the values in the array are not odd, the function should return false.
Examples:
hasOnlyOddNumbers([1,3,5,7]) // true
hasOnlyOddNumbers([1,2,3,5,7]) // false
*/
function hasOnlyOddNumbers(arr){
return arr.every(function(val){
return val % 2 !== 0;
});
}
/*
Write a function called hasNoDuplicates which accepts an array and returns true if there are no duplicate values (more than one element in the array that has the same value as another). If there are any duplicates, the function should return false.
Examples:
hasNoDuplicates([1,2,3,1]) // false
hasNoDuplicates([1,2,3]) // true
*/
// batter solution
function hasNoDuplicates(arr) {
return arr.every(function(val,i,array){
return array.indexOf(val) === arr.lastIndexOf(val);
})
}
// my solution
function hasNoDuplicates(arr){
// step 1 split the arr between the first and the rest.
// step 2 use every to check if any first value in the rest
// if true return step 1
var firstNumber = 0, isDup = true;
for (var i = arr.length; i > 1; i--) {
firstNumber = arr[0];
// arr.splice(0,1);
arr = arr.slice(1);
if(arr.every(function(val){
return val !== firstNumber;
}) === false) {
return false;
}
}
return true;
}
/*
Write a function called hasCertainKey which accepts an array of objects and a key, and returns true if every single object in the array contains that key. Otherwise it should return false.
Examples:
var arr = [
{title: "Instructor", first: 'Elie', last:"Schoppik"},
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
{title: "Instructor", first: 'Matt', last:"Lane"},
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
]
hasCertainKey(arr,'first') // true
hasCertainKey(arr,'isCatOwner') // false
*/
// use in method
function hasCertainKey(arr, key){
return arr.every(function(val){
return key in val;
})
}
// my solution
function hasCertainKey(arr, key){
return arr.every(function(val){
return val[key] !== undefined;
})
}
/*
Write a function called hasCertainValue which accepts an array of objects and a key, and a value, and returns true if every single object in the array contains that value for the specific key. Otherwise it should return false.
Examples:
var arr = [
{title: "Instructor", first: 'Elie', last:"Schoppik"},
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
{title: "Instructor", first: 'Matt', last:"Lane"},
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
]
hasCertainValue(arr,'title','Instructor') // true
hasCertainValue(arr,'first','Elie') // false
*/
function hasCertainValue(arr, key, searchValue){
return arr.every(function(val){
return val[key] === searchValue;
});
}
// Exercise: Reduce ====================================
/*
Write a function called extractValue which accepts an array of objects and a key and returns a new array with the value of each object at the key.
Examples:
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}]
extractValue(arr,'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractValue(arr, key){
return arr.reduce(function(acc, next){
acc.push(next[key]);
return acc;
},[]);
}
/*
Write a function called vowelCount which accepts a string and returns an object with the keys as the vowel and the values as the number of times the vowel appears in the string. This function should be case insensitive so a lowercase letter and uppercase letter should count
Examples:
vowelCount('Elie') // {e:2,i:1};
vowelCount('Tim') // {i:1};
vowelCount('Matt') // {a:1})
vowelCount('hmmm') // {};
vowelCount('I Am awesome and so are you') // {i: 1, a: 4, e: 3, o: 3, u: 1};
*/
function vowelCount(str){
var strToArr = str.toLowerCase().split('');
vowelStr = 'aeiou';
return strToArr.reduce(function(acc, next){
if(vowelStr.includes(next)){
if(next in acc){
acc[next]++;
} else {
acc[next] = 1;
}
}
return acc;
}, {})
}
/*
Write a function called addKeyAndValue which accepts an array of objects and returns the array of objects passed to it with each object now including the key and value passed to the function.
Examples:
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}];
addKeyAndValue(arr, 'title', 'Instructor') //
[
{title: 'Instructor', name: 'Elie'},
{title: 'Instructor', name: 'Tim'},
{title: 'Instructor', name: 'Matt'},
{title: 'Instructor', name: 'Colt'}
]
*/
function addKeyAndValue(arr, key, value){
return arr.reduce(function(acc, next, idx){
acc[idx][key] = value;
return acc;
}, arr)
}
/*
Write a function called partition which accepts an array and a callback and returns an array with two arrays inside of it. The partition function should run the callback function on each value in the array and if the result of the callback function at that specific value is true, the value should be placed in the first subarray. If the result of the callback function at that specific value is false, the value should be placed in the second subarray.
Examples:
function isEven(val){
return val % 2 === 0;
}
var arr = [1,2,3,4,5,6,7,8];
partition(arr, isEven) // [[2,4,6,8], [1,3,5,7]];
function isLongerThanThreeCharacters(val){
return val.length > 3;
}
var names = ['Elie', 'Colt', 'Tim', 'Matt'];
partition(names, isLongerThanThreeCharacters) // [['Elie', 'Colt', 'Matt'], ['Tim']]
*/
function partition(arr, callback){
return arr.reduce(function(acc, next){
if(callback(next)){
acc[0].push(next);
} else {
acc[1].push(next);
}
return acc;
},[[], []]);
}
// Closures exercise
/*
Write a function called specialMultiply which accepts two parameters. If the function is passed both parameters, it should return the product of the two. If the function is only passed one parameter - it should return a function which can later be passed another parameter to return the product. You will have to use closure and arguments to solve this.
Examples:
specialMultiply(3,4); // 12
specialMultiply(3)(4); // 12
specialMultiply(3); // function(){}....
*/
// my solution
function specialMultiply(a,b){
if(b) {
return a*b
} else {
return function(b) {
return b*a;
}
}
}
// offical solution
function specialMultiply(a,b) {
if(arguments.length === 1){
return function(b) {
return a*b;
}
}
return a*b;
}
/*
Write a function called guessingGame which takes in one parameter amount. The function should return another function that takes in a parameter called guess. In the outer function, you should create a variable called answer which is the result of a random number between 0 and 10 as well as a variable called guesses which should be set to 0.
In the inner function, if the guess passed in is the same as the random number (defined in the outer function) - you should return the string "You got it!". If the guess is too high return "Your guess is too high!" and if it is too low, return "Your guess is too low!". You should stop the user from guessing if the amount of guesses they have made is greater than the initial amount passed to the outer function.
You will have to make use of closure to solve this problem.
Examples (yours might not be like this, since the answer is random every time):
var game = guessingGame(5)
game(1) // "You're too low!"
game(8) // "You're too high!"
game(5) // "You're too low!"
game(7) // "You got it!"
game(1) // "You are all done playing!"
var game2 = guessingGame(3)
game2(5) // "You're too low!"
game2(3) // "You're too low!"
game2(1) // "No more guesses the answer was 0"
game2(1) // "You are all done playing!"
*/
// my solution
function guessingGame(amount){
var randomNumber = Math.floor(Math.random()*10);
var gotIt = false;
var i = 0;
return function(guessNumber) {
if (i === amount - 1 && gotIt === false) {
return "No more guesses the answer was " + randomNumber;
}
if (i > amount || gotIt === true) {
return 'You are all done playing!';
}
if(i < amount - 1) {
if(guessNumber === randomNumber) {
gotIt = true;
return 'You got it!';
} else if (guessNumber > randomNumber) {
return "You're too high!";
} else if (guessNumber < randomNumber) {
return "You're too low!";
}
i++;
}
}
}
// official solution
function guessingGame(amount){
var answer = Math.floor(Math.random()*11);
var guesses = 0;
var completed = false;
return function(guessNumber){
if(!completed){
guesses++;
if(guess === answer) {
completed = true;
return "You got it!"
}
else if(guess > answer) return "Your guess is too high!"
else if(guess < answer) return "Your guess is too low!"
else if(guesses === amount){
completed = true;
return "No more guesses the anser was " + answer;
}
}
return "All done playing!"
}
}
// Call, Apply and Bind exercise
/*
Write a function called arrayFrom which converts an array-like-object into an array.
Examples:
var divs = document.getElementsByTagName('divs');
divs.reduce // undefined
var converted = arrayFrom(divs);
converted.reduce // function(){}....
*/
function arrayFrom(arrayLikeObject){
//[].slice.apply(arrayLikeObject);
[].slice.call(arrayLikeObject);
}
/*
// Write a function called sumEvenArguments which takes all of the arguments passed to a function and returns the sum of the even ones.
Examples:
sumEvenArguments(1,2,3,4) // 6
sumEvenArguments(1,2,6) // 8
sumEvenArguments(1,2) // 2
*/
function sumEvenArguments(){
var arr = [];
for(var i = 0; i < arguments.length; i++) {
arr.push(arguments[i]);
}
var evenArr = arr.filter(function(val){
return val % 2 === 0;
});
return evenArr.reduce(function(acc, next){
return acc+next;
});
}
/*
Write a function called invokeMax which accepts a function and a maximum amount. invokeMax should return a function that when called increments a counter. If the counter is greater than the maximum amount, the inner function should return "Maxed Out"
Examples:
function add(a,b){
return a+b
}
var addOnlyThreeTimes = invokeMax(add,3);
addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(2,2) // 4
addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(1,2) // "Maxed Out!"
*/
function invokeMax(fn, num){
var counter = 0;
return function(){
if(counter === num) {
return "Maxed Out!";
} else {
counter++;
return fn.apply(this, arguments);
}
}
}
/*
Write a function called once which accepts two parameters, a function and a value for the keyword 'this'. Once should return a new function that can only be invoked once, with the value of the keyword this in the function set to be the second parameter.
Examples:
function add(a,b){
return a+b
}
var addOnce = once(add, this);
addOnce(2,2) // 4
addOnce(2,2) // undefined
addOnce(2,2) // undefined
function doMath(a,b,c){
return this.firstName + " adds " + (a+b+c)
}
var instructor = {firstName: "Elie"}
var doMathOnce = once(doMath, instructor);
doMathOnce(1,2,3) // "Elie adds 6"
doMathOnce(1,2,3) // undefined
*/
function once(fn, thisArg){
var isCalled = false;
return function(){
if(!isCalled){
isCalled = true;
return fn.apply(thisArg, arguments);
}
}
}
function add(a,b){
return a+b
}
var addOnce = once(add, this);
// BONUSES!
/*
Write a function called bind which accepts a function and a value for the keyword this. Bind should return a new function that when invoked, will invoke the function passed to bind with the correct value of the keyword this. HINT - if you pass more than two parameters to bind, those parameters should be included as parameters to the inner function when it is invoked. You will have to make use of closure!
Examples:
function firstNameFavoriteColor(favoriteColor){
return this.firstName + "'s favorite color is " + favoriteColor
}
var person = {
firstName: 'Elie'
}
var bindFn = bind(firstNameFavoriteColor, person);
bindFn('green') // "Elie's favorite color is green"
var bindFn2 = bind(firstNameFavoriteColor, person, 'blue');
bindFn2('green') // "Elie's favorite color is blue"
function addFourNumbers(a,b,c,d){
return a+b+c+d;
}
bind(addFourNumbers,this,1)(2,3,4) // 10
bind(addFourNumbers,this,1,2)(3,4) // 10
bind(addFourNumbers,this,1,2,3)(4) // 10
bind(addFourNumbers,this,1,2,3,4)() // 10
bind(addFourNumbers,this)(1,2,3,4) // 10
bind(addFourNumbers,this)(1,2,3,4,5,6,7,8,9,10) // 10
*/
function bind(fn, thisArg){
// pass the arguments of bind from index 2 to the rest
var outerArgs = [].slice.call(arguments, 2);
return function() {
var innerArgs = [].slice.call(arguments);
var allArgs = outerArgs.concat(innerArgs);
return fn.apply(thisArg, allArgs);
}
}
/*
Write a function called flip which accepts a function and a value for the keyword this. Flip should return a new function that when invoked, will invoke the function passed to flip with the correct value of the keyword this and all of the arguments passed to the function REVERSED. HINT - if you pass more than two parameters to flip, those parameters should be included as parameters to the inner function when it is invoked. You will have to make use of closure!
Examples:
function personSubtract(a,b,c){
return this.firstName + " subtracts " + (a-b-c);
}
var person = {
firstName: 'Elie'
}
var flipFn = flip(personSubtract, person);
flipFn(3,2,1) // "Elie subtracts -4"
var flipFn2 = flip(personSubtract, person, 5,6);
flipFn(7,8). // "Elie subtracts -4"
flip(subtractFourNumbers,this,1)(2,3,4) // -2
flip(subtractFourNumbers,this,1,2)(3,4) // -2
flip(subtractFourNumbers,this,1,2,3)(4) // -2
flip(subtractFourNumbers,this,1,2,3,4)() // -2
flip(subtractFourNumbers,this)(1,2,3,4) // -2
flip(subtractFourNumbers,this,1,2,3)(4,5,6,7) // -2
flip(subtractFourNumbers,this)(1,2,3,4,5,6,7,8,9,10) // -2
flip(subtractFourNumbers,this,11,12,13,14,15)(1,2,3,4,5,6,7,8,9,10) // -22
*/
function flip(fn, thisArg){
var outerArgs = [].slice.call(arguments, 2);
return function() {
var innerArgs = [].slice.call(argument);
//fn.length pass the argument number of fn function
var allArgs = outerArgs.concat(innerArgs).slice(0, fn.length);
return fn.apply(thisArg, allArgs.revers());
}
}
// Constructor functions exercise ==================================
// PART 1
// Create a constructor function for a Person, each person should have a firstName, lastName, favoriteColor and favoriteNumber. Your function MUST be named Person.
function Person(firstName, lastName, favoriteColor, favoriteNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.favoriteColor = favoriteColor;
this.favoriteNumber = favoriteNumber;
this.multiplyFavoriteNumber = function(number) {
return number * this.favoriteNumber;
}
}
// Write a method called multiplyFavoriteNumber that takes in a number and returns the product of the number and the object created from the Person functions' favorite number.
// PART 2
// Given the following code - refactor the Child function to remove all the duplication from the Parent function. You should be able to remove 4 lines of code in the Child function and replace it with 1 single line.
function Parent(firstName, lastName, favoriteColor, favoriteFood){
this.firstName = firstName;
this.lastName = lastName;
this.favoriteColor = favoriteColor;
this.favoriteFood = favoriteFood;
}
function Child(firstName, lastName, favoriteColor, favoriteFood){
Parent.apply(this, arguments);
}
// Prototype exercise
// - Create a constructor function for a Vehicle: every object created from this constructor should have a make, model, and year property. Each object should also have a property called isRunning, which should be set to false
// - Every object created from the Vehicle constructor should have a function called turnOn, which changes the isRunning property to true
// - Every object created from the Vehicle constructor should have a function called turnOff, which changes the isRunning property to false
// - Every object created from the Vehicle constructor should have a method called honk, which returns the string "beep" ONLY if the isRunning property is true
function Vehicle(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.isRunning = false;
}
Vehicle.prototype.turnOn = function() {
this.isRunning = true;
}
Vehicle.prototype.turnOff = function() {
this.isRunning = false;
}
Vehicle.prototype.honk = function() {
if(isRunning) {
return 'beep';
}
}
// 1 - Create a constructor function for a Person. Each person should have a firstName, lastName, favoriteColor, favoriteNumber)
function Person(firstName, lastName, favoriteColor, favoriteNumber){
this.firstName = firstName;
this.lastName = lastName;
this.favoriteColor = favoriteColor;
this.favoriteNumber = favoriteNumber;
this.family = [];
}
/* 2 - Add a function on the Person.prototype called fullName that returns the firstName and lastName property of an object created by the Person constructor concatenated together.
Examples:
var person = new Person("Elie", "Schoppik", "purple", 34)
person.fullName() // "Elie Schoppik"
*/
Person.prototype.fullName = function(){
return this.firstName + ' ' + this.lastName;
}
// 3 - Add a property on the object created from the Person function called family which is an empty array. This will involve you adding an additional line of code to your Person constructor.
/* 4 - Add a function on the Person.prototype called addToFamily which adds an object constructed from the Person constructor to the family array. To make sure that the object you are adding is an object construced from the Person constructor (HINT - take a look at the instanceof keyword). Make sure that your family array does not include duplicates! This method should return the length of the family array.
Examples:
var person = new Person("Elie", "Schoppik", "purple", 34)
var anotherPerson = new Person()
person.addToFamily(anotherPerson); // 1
person.addToFamily(anotherPerson); // 1
person.family.length // 1
person.addToFamily("test"); // 1
person.addToFamily({}); // 1
person.addToFamily([]); // 1
person.addToFamily(false); // 1
person.family.length // 1
*/
Person.prototype.addToFamily = function(personObj){
if(personObj instanceof Person && this.family.indexOf(personObj) === -1){
this.family.push(personObj);
}
return this.family.length;
}
// PART II
// 1 - Implement your own version of Array.prototype.map. The function should accept a callback and return a new array with the result of the callback for each value in the array.
Array.prototype.map = function(callback){
var newArr = [];
for(var i = 0; i < this.length; i++){
newArr.push(callback(this[i], i, this));
}
return newArr;
}
/* 2 - Implement a function called reverse that reverses a string and place it on the String.prototype
Examples:
"test".reverse() // "tset"
"tacocat".reverse() // "tacocat"
*/
// my solution
String.prototype.reverse = function(){
return this.split('').reverse().join('');
}
// offical solution
String.prototype.reverse = function(){
var newStr = '';
for(var i = this.length -1; i >= 0; i--){
newStr += this[i]
}
return newStr;
}
// Exercise: Inheritance ==========================
// 1 - Create a constructor function for a Vehicle. Each vehicle should have a make, model and year property.
function Vehicle(make, model, year){
this.make = make;
this.model = model;
this.year = year;
}
// 2 - Add a function to the Vehicle prototype called start which returns the string "VROOM!"