-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-stretches-solutions.js
1751 lines (1451 loc) · 45.4 KB
/
code-stretches-solutions.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
// CODE STRETCHES
/* ------------------------------------------------------------------------------------------------------------------ */
// Write a function called invert.
// This function takes in an obj, and returns an object whose key/value pairs are swapped.
const invert = (obj) => {
return Object.entries(obj).reduce((accumulator, entry) => {
accumulator[entry[1]] = `${entry[0]}`;
return accumulator;
}, {});
}
invert({x: 'foo', y: 3})
// {3: 'y', foo: 'x'}
invert({hello: 'world', bazz: 19})
// {19: 'bazz', world: 'hello'}
/* ------------------------------------------------------------------------------------------------------------------ */
// Write a function called sumValues.
// This function should sum all of the numeric values of an object.
const sumValues = (obj) => {
return Object.entries(obj).reduce((accumulator, entry) => {
if (typeof entry[1] === 'Number') {
accumulator += entry[1];
}
return accumulator;
}, 0);
};
console.log(sumValues({ foo: 1, bar: 2, bazz: 'whatever'}));
// 3
console.log(sumValues({}));
// 0
/* ------------------------------------------------------------------------------------------------------------------ */
// Write a function, intersect, that takes in two arrays,
// and returns an array populated by elements that are in both input arrays.
const intersect = (a, b) => {
return a.filter( x => b.includes(x));
}
const x = [1, 2, 3];
const y = [2, 3, 4, 5];
console.log(intersect(x, y))
// [2, 3]
// Bonus: perhaps a future implementation of intersectMany could be performing intersect
// on n arrays instead of just 2.
/* ------------------------------------------------------------------------------------------------------------------ */
// isMatch takes two objects.
// If the first object has all the properties of the second object return true,
// otherwise return false.
const isMatch = (target, matcher) => {
const entries = Object.entries(matcher);
for (let i=0; i < entries.length; i++) {
const [key, value] = entries[i];
if (target[key] !== value) {
return false;
}
}
return true;
}
isMatch({ foo: true, bar: 1}, { foo: true });
// true
isMatch({ foo: 42, bazz: 19}, {});
// true
isMatch({ foo: 42, bazz: 11}, { foo: 43 });
// false
isMatch({ }, { foo: 11 });
// false
/* ------------------------------------------------------------------------------------------------------------------ */
// multiMult is a function that takes any number of arguments and returns a function.
// The returned function takes a number,
// and returns the product of that number and the arguments passed in to multiMult.
const multiMult = (...args) => {
return (num) => {
return args.reduce((acc, item) => {
acc = acc * item;
return accumulator;
}, num);
};
};
const m5 = multiMult(5);
console.log(m5(4));
// 20
const m5_6 = multiMult(5, 6);
console.log(m5_6(3));
// 90
/* ------------------------------------------------------------------------------------------------------------------ */
// timer is a function that takes a number in seconds, and returns a function.
// The returned function performs a delayed action by the number of seconds initially passed in.
const timer = (delay) => {
return (fn) => {
setTimeout(fn, delay * 1000);
}
}
const t1= timer(2);
t1(() => console.log('2 sec'));
// This appears on the screen 2 seconds after called.
t1(() => console.log('2 sec'));
// This appears on the screen 2 seconds after called.
const t2 = timer(.5);
t2(() => console.log('.5 sec'));
// This appears on the screen 0.5 seconds after called.
const t3 = timer(0);
t3(() => console.log('0 sec'));
// This appears on the screen 0 seconds after called.
/* ------------------------------------------------------------------------------------------------------------------ */
// get passed an array and an object
// filter objects in array that have all the properties of the second argument
// note: an object can have extra properties
const fuzzyFilter = (arr, filter) => {
const isMatch = (item, filter) => {
const entries = Object.entries(filter);
let match = true;
entries.forEach(entry => {
const [key, value] = entry;
if (item[key] != value) {
match = false;
};
});
return match;
};
return arr.filter( item => {
isMatch(item, filter)
});
};
console.log(fuzzyFilter([ { foo: 'bar', x: 3},
{ foo: 'bar'},
{ foo: 'ba', y: 1} ],
{ foo: 'bar' }));
// [ { foo: 'bar', x: 3 }, { foo: 'bar' } ]
console.log(fuzzyFilter([ { foo: 'bar', x: 3},
{ foo: 'bar'},
{ foo: 'bar', y: 1} ],
{ foo: 'ba' }));
// []
/* ------------------------------------------------------------------------------------------------------------------ */
// write the fill function
// takes two arguments
// first determines length of returned array
// second takes the value the array should be filled with
// note the behavior where the 2nd parameter is an object
const fill = (count, value) => {
// we're pushing in the same object each iteration
// which works for a1, and a2, but not for a3
// return Array(count).fill(value);
const results = [];
while (results.length < count) {
// by using the spread operator, we create an entirely
// new object instead of pointing to the same object
results.push(typeof value === 'object' ? {...value} : value);
}
return results;
}
const a1 = fill(3, true);
console.log(a1);
// [true, true, true]
const a2 = fill(4,42)
console.log(a2);
// [42, 42, 42]
const x = {foo: 'bar'}
const a3 = fill(2, x);
console.log(a3[0] === a3[1])
// false
/* ------------------------------------------------------------------------------------------------------------------ */
// randomList is passed an array and a count
// it returns an array of values chosen randomly from list with no duplicates
const randomList = (list, count) => {
const ret = [];
while (ret.length < count) {
const idx = Math.floor(Math.random()) * list.length;
const item = list[idx];
if (!ret.includes(item)) {
ret.push(item);
}
}
return ret;
}
console.log(randomList([1,2,3], 2));
// random choice of two like [1,2] [3,1] [2,1]
console.log(randomList(['foo', 'bar', 'bazz', 'quq'], 2));
// random choice of two
/* ------------------------------------------------------------------------------------------------------------------ */
// getVowels takes in a string and returns a non-repeating array of the vowels in the string
const getVowels = (word) => {
return Object.keys(word.split('')
.reduce((acc, ltr) => {
if ('aeiou'.includes(ltr)) {
acc[ltr] = true;
}
return acc;
}, []));
}
console.log(getVowels('Fullstack Academy'))
// ['u', 'a', 'e']
/* ------------------------------------------------------------------------------------------------------------------ */
// processor takes in parentList, childList, listName, primaryKey, foreignKey
// processor iterates through the parentList and adds listName as a key to each object in the parentList
// listName will have an array as its value populated by objects
// from the childList whose foreignKey matches the primaryKey from parentList
const processor = (parentList, childList, listName, primaryKey, foreignKey) => {
return parentList.map( parentItem => {
const list = childList.filter(childItem => {
return childItem[foreignKey] === parentItem[primaryKey]
});
// we "splat" the parentItem
return {...parentItem, [listName]: list};
})
}
const users = [
{ id: 'x', name: 'Moe' },
{ id: 'y', name: 'Larry' },
]
const things = [
{ id: 'a', userId: 'x', name: 'foo'},
{ id: 'b', userId: 'x', name: 'bar'},
{ id: 'c', userId: 'y', name: 'bazz'},
];
const processed = processor(users, things, 'things', 'id', 'userId');
console.log(JSON.stringify(processed, null, 2));
// "[
// {
// "id": "x",
// "name": "Moe",
// "things": [
// {
// "id": "a",
// "userId": "x",
// "name": "foo"
// },
// {
// "id": "b",
// "userId": "x",
// "name": "bar"
// }
// ]
// },
// {
// "id": "y",
// "name": "Larry",
// "things": [
// {
// "id": "c",
// "userId": "y",
// "name": "bazz"
// }
// ]
// }
// ]"
/* ------------------------------------------------------------------------------------------------------------------ */
// objFromArray takes in an array and returns an object,
// where the returned object is populated with keys from the odd elements in the array
// and values from the even elements in the array
const objFromArray = (arr) => {
return arr.reduce((acc, value, idx) => {
if (idx % 2 === 0) {
acc[value] = arr[idx + 1];
}
return acc;
}, {});
};
console.log(objFromArray(['foo', 2, 'bar', 4]))
// { foo: 2, bar: 4 }
console.log(objFromArray([1, 2, 3, 4, 5, 6]))
// { '1': 2, '3': 4, '5': 6 }
/* ------------------------------------------------------------------------------------------------------------------ */
// it removes the odd values from an array and returns the number of items it removed.
// WRONG SOLUTION. WE NEED TO MAKE THIS AN IMPURE FUNCTION
const removeOdd = (input) => {
let count = 0;
const arr = input.filter( (input, index, arr) => {
if (el % 2 === 0) {
return el;
} else count++;
})
return count;
}
const arr = [1, 3, 5, 8, 10, 41];
console.log(removeOdd(arr));
// 4
console.log(arr);
// [8, 10]
/* ------------------------------------------------------------------------------------------------------------------ */
// returns true if one of the values in the object contains the passed in string
// Search is case insensitive
// returns false otherwise
const objContainsString = (obj, target) => {
// since we are case insensitive, lowercase the target and
// lowercase the values when we reach them
target = target.toLowerCase()
const values = Object.values(obj)
for (let i=0; i<values.length; i++) {
if (values[i].toLowerCase().includes(target)) {
return true;
}
}
return false;
}
console.log(objContainsString({ x: 'Foo', b: 'bare' }, 'foo'));
// true
console.log(objContainsString({ x: 'Foo', b: 'bare' }, 'bar'));
// true
console.log(objContainsString({ one: 'Foo', two: 'bare' }, 'bazz'));
// false
console.log(objContainsString({ }, 'whatever'));
// false
/* ------------------------------------------------------------------------------------------------------------------ */
// A function that groups items in an array
const group = (arr, key) => {
return arr.reduce((acc, obj) => {
if (acc[obj[key]] === undefined) {
acc[obj[key]] = [obj];
return acc;
} else {
acc[obj[key]] = [...acc[obj[key]], obj];
return acc;
}
}, {});
}
const data = [
{
name: 'foo',
id: 7
},
{
name: 'foo',
id: 8
},
{
name: 'bar',
id: 8
}
];
console.log(group(data, 'name'));
/*
{ foo: [ { name: 'foo', id: 7 }, { name:'foo', id: 8 } ],
bar: [ { name: 'bar', id: 8 } ] }
*/
console.log(group(data, 'id'));
/*
{ '7': [ { name: 'foo', id: 7 } ],
'8': [ { name: 'foo', id: 8 }, { name:'bar', id: 8 } ] }
*/
/* ------------------------------------------------------------------------------------------------------------------ */
// Creates an array of grouped elements
// the first of which contains the first elements of the given arrays,
// the second of which contains the second elements of the given arrays, and so on.
// accepts any number of arrays
const zip = (...arr) => {
const results = [];
arr.forEach( list => {
list.forEach((item, idx) => {
results[idx] = results[idx] || [];
results[idx].push(item);
});
});
return results;
};
console.log(zip(['a', 'b'], [1, 2], [true, false]))
// => [['a', 1, true], ['b', 2, false]]
console.log(zip([]))
// => []
/* ------------------------------------------------------------------------------------------------------------------ */
// will return an array, length determined by passed in value
// array starts at one and each value increments by one,
// however, if the number is divisible by 3 and 5 replace number with FIZZBUZZ
// if number is divisible by 3 return FIZZ
// if number is divisible by 5 return BUZZ
const fizzBuzz = (num) => {
const output = [];
for (let i=1; i<num + 1; i++) {
if ((i % 3 === 0) && (i % 5 === 0)) {
output.push('FIZZBUZZ')
} else if (i % 3 === 0) {
output.push('FIZZ')
} else if (i % 5 === 0) {
output.push('BUZZ')
} else {
output.push(i)
}
}
return output;
}
console.log(fizzBuzz(15));
/*
[ 1,
2,
'FIZZ',
4,
'BUZZ',
'FIZZ',
7,
8,
'FIZZ',
'BUZZ',
11,
'FIZZ',
13,
14,
'FIZZBUZZ' ]
*/
/* ------------------------------------------------------------------------------------------------------------------ */
// Creates an object composed of the picked object properties.
const pick = (obj, keys) => {
return keys.reduce( (acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
// const output = {};
// arr.forEach( el => {
// if (obj[el] !== undefined) {
// output[el] = obj[el];
// } else {
// output[el] = undefined;
// }
// });
// return output;
}
var object = { 'a': 1, 'b': '2', 'c': 3 };
pick(object, ['a', 'c'])
// => { 'a': 1, 'c': 3 }
pick(object, ['d']);
// => { d: undefined}
/* ------------------------------------------------------------------------------------------------------------------ */
const average = (arr, key) => {
let counter = 0;
const total = arr.reduce((acc, obj) => {
// we should not assume that each obj in the array has the key
// and we should not assume that each value is a number should the object have the key
if (obj[key] !== undefined && typeof obj[key] === 'number') {
counter++;
return acc + obj[key];
}
return acc;
}, 0);
return total / counter;
};
const testScores = [
{ id: 1, score: 80 },
{ id: 2, score: 90 },
{ id: 3, score: 100 }
];
average(testScores, 'score');
// 90
const otherTestScores = [
{ id: 1, total: 80 },
{ id: 2, total: 60 },
{ id: 3, total: 100 }
];
average(otherTestScores, 'total');
// 80
/* ------------------------------------------------------------------------------------------------------------------ */
const multString = (str) => {
// javascript is very nice and will convert numbers for us
return str.split(',').reduce( (acc, value) => {
return acc * value;
}, 1);
// const arr = str.split(',');
// return arr.reduce((prod, el) => {
// // we cannot assume that all elements we convert to numbers will actually be numbers
// const intEl = parseInt(el, 10);
// // so we only multiply the product by the number casted element if it is a number and not NaN
// if ((typeof intEl === 'number') && (!Number.isNaN(intEl))) {
// return prod *= intEl;
// } else {
// return prod;
// }
// }, 1);
};
console.log(multString('2, 5, 3'));
// 30
console.log(multString('2, 2, 2'));
// 8
// Bonus
console.log(multString('yo, hi, 3'));
//3
/* ------------------------------------------------------------------------------------------------------------------ */
// passed in string, counts occurences of each letter
const letters = 'abcdefghijklmnopqrstuvwxyz';
const letterCounter = str => {
return str.split('').reduce( (acc, letter) => {
acc[letter] = acc[letter] || 0;
acc[letter]++;
return acc;
}, {})
// return str.split('').reduce( (freq, letter) => {
// // we only want to add to the frequency object if we reach a letter
// if (letters.includes(letter)) {
// if (freq[letter] === undefined) {
// freq[letter] = 1;
// } else {
// freq[letter]++;
// };
// };
// return freq;
// }, {});
};
console.log(letterCounter('abc'));
// { a: 1, b: 1, c: 1 }
console.log(letterCounter('foobarbazz'));
// { f: 1, o: 2, b: 2, a: 2, r: 1, z: 2 }
// Bonus
console.log(letterCounter('123'));
// {}
/* ------------------------------------------------------------------------------------------------------------------ */
const delay = (ms, str) => {
return new Promise((resolve) => {
setTimeout( () => {
resolve(str)
}, ms);
});
};
const delayFail = (ms, str) => {
return new Promise((resolve, reject) => {
setTimeout( () => {
reject(str);
}, ms);
});
};
delay(500, 'foo')
.then((result) => console.log(result));
// logs foo in half a second
delayFail(1000, 'bar')
.catch( ex => console.log(ex));
// logs bar in a second
/* ------------------------------------------------------------------------------------------------------------------ */
// you cannot use the array reduce method within reduce
const reduce = (arr, func, start) => {
let returnValue = start;
arr.forEach( val => {
returnValue = func(returnValue, val)
});
return returnValue;
};
console.log(reduce([1, 2, 3], (acc, item)=> {
return item * acc;
}, 2));
// 12
console.log(reduce([1, 2, 3, 4], (acc, item)=> {
return item * acc;
}, 10));
// 240
/* ------------------------------------------------------------------------------------------------------------------ */
const mostPopularLetter = (str) => {
const counter = str.split('').reduce( (acc, letter) => {
acc[letter] = acc[letter] || 0;
acc[letter]++;
return acc;
}, {});
let popularLetter = '';
let popularLetterCount = 0;
Object.keys(counter).map( (key) => {
if (counter[key] > popularLetterCount) {
popularLetterCount = counter[key];
popularLetter = key;
};
});
return popularLetter;
};
console.log(mostPopularLetter('foo'));
// o
console.log(mostPopularLetter('foobarbazbz'));
// b
console.log(mostPopularLetter(''));
// undefined
/* ------------------------------------------------------------------------------------------------------------------ */
const group = (arr, fn) => {
return arr.reduce( (acc, el) => {
let category = fn(el);
acc[category] = acc[category] || [];
acc[category].push(el);
return acc;
}, {});
};
let grouped = group([1, 2, 3], (i)=> i % 2 ? 'odd' : 'even');
console.log(grouped);
// { odd: [ 1, 3 ] , even: [ 2 ] }
grouped = group([1, 2, 3], (i)=> i >= 2 ? 'gte 2' : 'lt2');
console.log(grouped);
// { lt2: [ 1 ], 'gte 2': [ 2, 3 ] }
grouped = group(['a', 'b', 1, 2, 3],(i) => typeof i === 'number' ? 'numbers' : 'not numbers');
console.log(grouped);
// { 'not numbers': [ 'a', 'b' ], numbers: [ 1, 2, 3 ] }
/* ------------------------------------------------------------------------------------------------------------------ */
const groupByDataType = arr => {
return arr.reduce( (output, el) => {
output[typeof el] = output[typeof el] || [];
output[typeof el].push(el);
return output;
}, {});
};
console.log(groupByDataType([1, 2, 3]));
// { number: [ 1, 2, 3 ] }
console.log(groupByDataType([1, 'a', 'b', true, false, [], new Date(), true, {}]));
/* { number: [ 1 ],
string: [ 'a', 'b' ],
boolean: [ true, false, true ],
object: [ [], 2020-03-03T20:54:30.720Z, {} ] } */
// date will be current date!
/* ------------------------------------------------------------------------------------------------------------------ */
const size = (thing) => {
if (Array.isArray(thing)) {
return thing.length;
} else {
return Object.keys(thing).length;
};
};
console.log(size([1, 2, 3, 4, 5]));
// 5
console.log(size({one: 1, two: 2, three: 3}));
// 3
console.log(size({one: 1, two: 2, three: 3, foo: 'bar'}));
// 4
/* ------------------------------------------------------------------------------------------------------------------ */
const partition = (arr, fn) => {
return arr.reduce( (acc, el) => {
if (fn(el)) {
acc[0].push(el);
} else {
acc[1].push(el);
};
return acc;
}, [ [], [] ]);
};
console.log(partition([1, 2, 3, 4], (x)=> x % 2 === 1));
// [ [ 1, 3 ], [ 2, 4 ] ]
console.log(partition([1, 2, 3, 4], (x)=> typeof x === 'string'));
// [ [], [ 1, 2, 3, 4 ] ]
console.log(partition([1, 'a', 'b', 2, 3, 4], (x)=> typeof x === 'string'));
// [ [ 'a', 'b' ], [ 1, 2, 3, 4 ] ]
/* ------------------------------------------------------------------------------------------------------------------ */
const pluck = (arr, key) => {
return arr.reduce( (acc, obj) => {
if (obj[key]) {
acc.push(obj[key]);
};
return acc;
}, []);
};
const stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
console.log(pluck(stooges, 'name'));
// [ 'moe', 'larry', 'curly' ]
/* ------------------------------------------------------------------------------------------------------------------ */
// receives an array and returns an object
// all array's are grouped with key array and everything else is grouped under other
const separate = (arr) => {
return arr.reduce( (acc, el) => {
Array.isArray(el) ? acc.arrays.push(el) : acc.other.push(el);
return acc;
}, { arrays: [], other: [] });
};
console.log(separate([{},1, [1, 2], ['a'], {}]));
// { arrays: [ [ 1, 2 ], [ 'a' ] ], other: [ {}, 1, {} ] }
/* ------------------------------------------------------------------------------------------------------------------ */
// you can round up or down to the closest number of days
// a date in future should return a positive number
// a date in the past should return a negative number
const daysFromNow = (day) => {
const today = new Date();
const msInADay = 1000 * 60 * 60 * 24;
const diff = Math.round((day - today) / msInADay);
return diff;
}
console.log(daysFromNow(new Date('07/04/2020')));
// logs days from now
/* ------------------------------------------------------------------------------------------------------------------ */
// isMatch returns true if the first object has all the same keys and values of second object
// note the first object can have extra properties, yet still return true
const isMatch = (objA, objB) => {
const entriesB = Object.entries(objB);
for (let i=0; i<entriesB.length; i++) {
const [ k, v ] = entriesB[i];
if (objA[k] !== v) {
return false;
};
};
return true;
};
console.log(isMatch({}, {x: 1}));
// false
console.log(isMatch({x: 1, y: 2}, {x: 1}));
// true
console.log(isMatch({x: 1, y: 2, z: 3}, {x: 1, y: 2}));
// true
console.log(isMatch({x: 1, y: 2, z: 3}, {x: 1, y: 1}));
// false
/* ------------------------------------------------------------------------------------------------------------------ */
const letterCounter = str => {
return str.split('').reduce( (acc, el) => {
acc[el] = acc[el] || 0;
acc[el]++;
return acc;
}, {});
};
console.log(letterCounter('abcdefhi a b c'));
// { a: 2, b: 2, c: 2, d: 1, e: 1, f: 1, h:1, i: 1, ' ': 3 }
/* ------------------------------------------------------------------------------------------------------------------ */
const uniqueConsonant = str => {
return str.split('').reduce( (acc, curr) => {
if (!acc.includes(curr) && !'aeiou'.includes(curr)) {
acc.push(curr);
};
return acc;
}, []);
};
console.log(uniqueConsonant('abacub'));
// [ 'b', 'c' ]
/* ------------------------------------------------------------------------------------------------------------------ */
const tempConvertor = (tempObj) => {
const { degree, scale } = tempObj;
const convertedTempObj = {};
if (scale === 'F') {
convertedTempObj.degree = ( (5/9) * ( degree - 32) );
convertedTempObj.scale = 'C';
} else if (scale === 'C') {
convertedTempObj.degree = ( ( (9/5) * degree) + 32);
convertedTempObj.scale = 'F';
};
return convertedTempObj;
};
console.log(tempConvertor({ degree: 32, scale: 'F'}));
// { degree: 0, scale: 'C' }
console.log(tempConvertor({ degree: 212, scale: 'F'}));
// { degree: 100, scale: 'C' }
console.log(tempConvertor({ degree: 0, scale: 'C'}));
// { degree: 32, scale: 'F' }
console.log(tempConvertor({ degree: 100, scale: 'C'}));
// { degree: 212, scale: 'F' }
/* ------------------------------------------------------------------------------------------------------------------ */
const tempConvertorErrorHandling = tempObj => {
if (!['F', 'C'].includes(tempObj.scale) || typeof tempObj.degree !== 'number') {
throw('scale must be F or C');
};
if (tempObj.scale === 'F') {
const temp = (5/9) * (tempObj.degree - 32);
return { degree: temp, scale: 'C' };
} else if (tempObj.scale === 'C') {
const temp = ( (9/5) * tempObj.degree ) + 32;
return { degree: temp, scale: 'F' };
};
};
console.log(tempConvertorErrorHandling({ degree: 32, scale: 'F'}));
// { degree: 0, scale: 'C' }
console.log(tempConvertorErrorHandling({ degree: 212, scale: 'F'}));
// { degree: 100, scale: 'C' }
console.log(tempConvertorErrorHandling({ degree: 0, scale: 'C'}));
// { degree: 32, scale: 'F' }
console.log(tempConvertorErrorHandling({ degree: 100, scale: 'C'}));
// { degree: 212, scale: 'F' }
try {
console.log(tempConvertorErrorHandling({ degree: 100, scale: 'X'}));
}
catch(e){
console.log(e);
}
// scale must be F or C
try {
console.log(tempConvertorErrorHandling({ degree: '32', scale: 'F'}));
}
catch(e){
console.log(e);
}
// scale must be F or C
/* ------------------------------------------------------------------------------------------------------------------ */
const rejectMeInCaps = str => {
throw(str.toUpperCase());
}
rejectMeInCaps('foo')
.catch( result => console.log(result));
// FOO
rejectMeInCaps('bar')
.catch( result => console.log(result));
// BAR
/* ------------------------------------------------------------------------------------------------------------------ */
const resolveMeInUpperCase = str => {
return new Promise( (res, rej) => {
return res(str.toUpperCase());
});
};
resolveMeInUpperCase('foo')
.then( result => console.log(result));
// FOO
resolveMeInUpperCase('bar')
.then( result => console.log(result));
// BAR
/* ------------------------------------------------------------------------------------------------------------------ */
const onlyEven = (num) => {
return new Promise( (res, rej) => {
if (num % 2 === 0) {
res(num);
} else {
rej(num);
};
});
};
onlyEven(2)
.then(num=> console.log(`resolved with ${num}`));
onlyEven(3)
.catch(num=> console.log(`rejected with ${num}`));
/*
resolved with 2
rejected with 3
*/