-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntermediateAlgorithmScripting.js
913 lines (840 loc) · 36.5 KB
/
IntermediateAlgorithmScripting.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
// ================== Sum All Numbers in a Range ===================
// We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.
function sumAll(arr) {
let sortArr = [...arr].sort((a, b) => a - b); // Sort
let sum = 0;
for (let i = sortArr[0]; i <= sortArr[1]; i++) {
sum += i;
}
return sum;
}
console.log(sumAll([4, 1])); // 10
// fCC recursive solution
function sumsAll(arr) {
const [first, last] = [...arr].sort((a, b) => a - b);
return first !== last
? first + sumsAll([first + 1, last])
: first;
}
console.log(sumsAll([1, 4])); // 10
// ================== Diff Two Arrays ===================
// Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
// My imperative solution
function diffArray(arr1, arr2) {
const newArr = [];
for (let num of arr1) {
if (arr2.indexOf(num) === -1) {
newArr.push(num);
}
}
for (let num of arr2) {
if (arr1.indexOf(num) === -1) {
newArr.push(num);
}
}
return newArr;
}
console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])); // [ 4 ]
console.log(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "George", "andesite", "grass", "dirt", "dead shrub"])); // [ 'pink wool', 'George' ]
// fCC declarative solution
function diffArr(arr1, arr2) {
return arr1
.concat(arr2)
.filter(item => !arr1.includes(item) || !arr2.includes(item));
}
console.log(diffArr([1, 2, 3, 5, 7], [1, 2, 3, 4, 5])); // [ 7, 4 ]
// And from fCC forum belcurv
function diffArra(arr1, arr2) {
// filter each array for the absent members
var filteredArr1 = arr1.filter((el) => arr2.indexOf(el) === -1),
filteredArr2 = arr2.filter((el) => arr1.indexOf(el) === -1);
// merge both sets of absent members
return filteredArr1.concat(filteredArr2);
}
console.log(diffArra([1, 2, 3, 5, 7], [1, 2, 3, 4, 5])); // [ 7, 4 ]
// ================== Seek and Destroy ===================
/* You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
Note: You have to use the arguments object. (Nope) */
// 1st successful
function destroyer(arr, ...others) {
let newArr = [];
arr.forEach(function (elem) {
if (others.indexOf(elem) === -1) {
newArr.push(elem);
}
});
return newArr;
}
// 2nd, better
function destroyer(arr, ...others) {
return arr.filter(elem => others.indexOf(elem) === -1);
}
console.log(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)); // [ 1, 5, 1 ]
console.log(destroyer(["tree", "hamburger", 53], "tree", 53)); // [ 'hamburger' ]
// fCC
function destroyer(arr, ...valsToRemove) {
return arr.filter(elem => !valsToRemove.includes(elem));
}
console.log(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan")); // [ 12, 92, 65 ]
// ================== Wherefore art thou ===================
// Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument).
// 1st successful w/console.logs
function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
let sourceKeys = Object.keys(source);
// console.log(sourceKeys);
collection.forEach(function (obj) {
// let objKeys = Object.keys(obj);
// console.log(objKeys);
for (let key of sourceKeys) {
// console.log(key);
// console.log(obj[key]);
// console.log(source[key]);
if (obj[key] !== source[key]) {
return false;
}
}
arr.push(obj);
})
// Only change code above this line
return arr;
}
// 2nd, better
function whatIsInAName(collection, source) {
return collection.filter(obj => Object.keys(source).every(key => obj[key] === source[key]));
}
console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })); // [ { apple: 1, bat: 2 }, { apple: 1, bat: 2, cookie: 2 } ]
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })); // [ { first: 'Tybalt', last: 'Capulet' } ]
// ================== Spinal Tap Case ===================
// Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
// Mine using regex
function spinalCase(str) {
return str
.replace(/([A-Z])/g, " $1")
.trim()
.replace(/\s+/g, "-")
.replace(/_/g, "")
.toLowerCase();
}
// Or fCC
function spinalCase(str) {
return str
/* Split the string (into an array) at one of the following conditions
1. A whitespace character [\s] is encountered
2. Underscore character [_] is encountered
3. Or is followed by an uppercase letter [(?=[A-Z])] */
.split(/\s|_|(?=[A-Z])/)
// Join the array using a hyphen (-)
.join("-")
// Lowercase the whole resulting string
.toLowerCase();
}
// from fCC forum t0cc
function spinalCase(str) {
return str.replace(/(\w)[ _]?([A-Z])| /g, "$1-$2").toLowerCase();
}
console.log(spinalCase("AllThe-small Things"));
console.log(spinalCase("The_Andy_Griffith_Show"));
console.log(spinalCase('This Is Spinal Tap'));
// ================== Pig Latin ===================
/* Pig Latin is a way of altering English Words. The rules are as follows:
1. If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add ay to it.
2. If a word begins with a vowel, just add way at the end. */
function translatePigLatin(str) {
let splitStr = str.split("");
let regex = /[aeiou]/g;
if (!str.match(regex)) { // If the string doesn't have any vowels in it
splitStr.push("ay");
} else if (splitStr[0].match(regex)) { // If the first letter is a vowel
splitStr.push("way");
} else {
let count = 0;
while (!splitStr[0].match(regex) && count < 4) {
splitStr.push(splitStr[0])
splitStr.shift();
count++;
}
splitStr.push("ay");
}
return splitStr.join('');
}
// fCC
function translatePigLatin(str) {
return str
.replace(/^[aeiou]\w*/, "$&way") // If the string begins with a vowel, "$&" returns the whole match and "way" is added to it, otherwise...
.replace(/(^[^aeiou]+)(\w*)/, "$2$1ay"); // If it begins with one or more non-vowels followed by zero or more alphanumerics, the consonants get moved to the end of the word and "ay" is added to it
}
// from fCC forum rafaeltikva
function translatePigLatin(str) {
return str.match(/^([^aeiou]+)/) ? str.replace(/^([^aeiou]+)(\w+)?/, "$2$1ay") : str + "way";
}
// from fCC forum RainierXS
function translatePigLatin(str) {
if (str.match(/^[aeiou]/)) { //starts with vowel
return str.replace(/(.+)/, "$1way");
} else if (str.match(/[aeiou]/g)) { //doesn't start with a vowel but has a vowel in it
return str.replace(/(^[^aeiou]+)(.+)/g, "$2$1ay");
} else { //all consonants
return str + "ay";
}
}
// RainierXS solution converted to ternary
function translatePigLatin(str) {
return str.match(/^[aeiou]/)
? str.replace(/(.+)/, "$1way")
: str.match(/[aeiou]/g)
? str.replace(/(^[^aeiou]+)(.+)/g, "$2$1ay")
: str + "ay";
}
console.log(translatePigLatin("algorithm")); // algorithmway
console.log(translatePigLatin("schwartz")); // artzschway
console.log(translatePigLatin("rhythm")); // rhythmay
console.log(translatePigLatin("consonant")); // onsonantcay
console.log(translatePigLatin("glove")); // oveglay
// ================== Search and Replace ===================
// Perform a search and replace on the sentence using the arguments provided and return the new sentence. Note: Preserve the case of the first character in the original word when you are replacing it.
function myReplace(str, before, after) {
return str.replace(before, /[a-z]/.test(before[0]) ? after.toLowerCase() : after[0].toUpperCase() + after.slice(1));
}
console.log(myReplace("A quick brown fox jumped over the lazy dog", "jumped", "Leaped")); // A quick brown fox leaped over the lazy dog
console.log(myReplace("Let us get back to more Coding", "Coding", "algorithms")); // Let us get back to more Algorithms
console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting")); // He is Sitting on the couch
console.log(myReplace("His name is Tom", "Tom", "john")); // His name is John
// ================== DNA Pairing ===================
/* The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2D array.
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
Return the provided character as the first element in each array.
For example, for the input GCG, return [["G", "C"], ["C","G"], ["G", "C"]]
The character and its pair should be paired up in an array, and all the arrays grouped into one encapsulating array. */
// 1st successful
function pairElement(str) {
const strArr = [...str];
let dnaArr = [];
for (let elem of strArr) {
if (elem === "A") dnaArr.push(["A", "T"]);
else if (elem === "T") dnaArr.push(["T", "A"]);
else if (elem === "C") dnaArr.push(["C", "G"]);
else if (elem === "G") dnaArr.push(["G", "C"]);
else return "DNA strand input error";
}
return dnaArr;
}
// 2nd hybrid with a "for of" array loop and "switch case"
function pairElement(str) {
const strArr = [...str];
let dnaArr = [];
for (let elem of strArr) {
switch (elem) {
case 'A': dnaArr.push(["A", "T"]);
break;
case 'T': dnaArr.push(["T", "A"]);
break;
case 'C': dnaArr.push(["C", "G"]);
break;
case 'G': dnaArr.push(["G", "C"]);
}
}
return dnaArr;
}
// fCC almost the same as the solution from "jori" below
function pairElement(str) {
//create object for pair lookup
var pairs = {
A: "T",
T: "A",
C: "G",
G: "C"
};
//split string into array of characters
var arr = str.split("");
//map character to array of character and matching pair
return arr.map(x => [x, pairs[x]]);
}
// from fCC forum emo1
function pairElement(str) {
return str.split("").map(function (el) {
if (el === "A") return ["A", "T"];
else if (el === "T") return ["T", "A"];
else if (el === "C") return ["C", "G"];
else if (el === "G") return ["G", "C"];
});
}
// and fCC forum jori
function pairElement(str) {
const mapping = { 'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C' };
return str.split('').map(item => [item, mapping[item]]);
}
console.log(pairElement("GCG"));
console.log(pairElement("TTGAG"));
console.log(pairElement("TTGbG"));
// ================== Missing letters ===================
// Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
// 1st successful w/console.logs
function fearNotLetter(str) {
const alpha = 'abcdefghijklmnopqrstuvwxyz'.split('');
let strArr = str.split('');
let beginInd = alpha.indexOf(strArr[0]);
console.log(alpha.indexOf(strArr[0]));
let endInd = alpha.indexOf(strArr[strArr.length - 1]);
console.log(alpha.indexOf(strArr[strArr.length - 1]));
console.log(strArr.length - 1);
for (let i = beginInd; i < endInd; i++) {
console.log(alpha[i]);
if (strArr.indexOf(alpha[i]) < 0) {
return alpha[i];
}
}
}
// 2nd refined version of 1st
function fearNotLetter(str) {
const alpha = 'abcdefghijklmnopqrstuvwxyz';
let beginInd = alpha.indexOf(str[0]);
let endInd = alpha.indexOf(str[str.length - 1]);
for (let i = beginInd; i < endInd; i++) {
if (str.indexOf(alpha[i]) < 0) {
return alpha[i];
}
}
}
console.log(fearNotLetter("abcde")); // undefined
console.log(fearNotLetter("bcdf")); // e
console.log(fearNotLetter("efghikl")); // j
// from fCC forum benjaminadk
/* Explanation
1. So since the char-codes go in order I just compared the code of the current letter + 1 to the code of the next letter
2. If those two values aren’t equal I return the letter that should be there
3. If nothing qualifies for my if statement the function automatically returns undefined so I don’t even have to write that in the code
4. Having the loop stop at str.length-1 was key here
Javascript just blows my mind sometimes. this was just the first thing that came to mind and the fact that it works makes me psyched to do more */
// With console.logs added
function fearNotLetter(str) {
for (let i = 0; i < str.length - 1; i++) {
console.log(str.charCodeAt(i) + 1);
console.log(str.charCodeAt(i + 1));
if (str.charCodeAt(i) + 1 !== str.charCodeAt(i + 1)) {
return String.fromCharCode(str.charCodeAt(i) + 1);
}
}
}
console.log(fearNotLetter("abcde"));
console.log(fearNotLetter("bcdf"));
console.log(fearNotLetter("efghikl"));
// ================== Sorted Union ===================
/* Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order. */
// 1st successful
function uniteUnique(...arrs) {
let newArr = [];
let flatArr = arrs.flat();
for (let num of flatArr) {
if (!newArr.includes(num)) {
newArr.push(num);
}
}
return newArr;
}
// 2nd better
function uniteUnique(...arrs) {
let newArr = [];
let flatArr = arrs.flat();
return flatArr.filter(num => !newArr.includes(num) ? newArr.push(num) : null);
}
// 3rd better still
function uniteUnique(...arrs) {
let flatArr = arrs.flat(); // Can't chain for some reason
return flatArr.filter((num, index) => flatArr.indexOf(num) === index);
}
// Or using the (formerly unknown) "Set". added May 24, 2022
/* A Set is a collection of unique values. To remove duplicates from an array:
1. Convert an array of duplicates to a Set. The new Set will implicitly remove duplicate elements.
2. Convert the set back to an array. */
// Like this
function uniteUnique(...arrs) {
return [...new Set(arrs.flat())];
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])); // [ 1, 3, 2, 5, 4 ]
// Not this
function uniteUnique(...arrs) {
return [new Set(arrs.flat())];
}
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])); // [ Set(5) { 1, 3, 2, 5, 4 } ]
// ================== Convert HTML Entities ===================
// Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
// 1st successful
function convertHTML(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// from fCC forum EvanMorrison
function convertHTML(str) {
var entities = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }
return str.replace(/[&<>"']/g, function (match) {
return entities[match]
})
}
console.log(convertHTML('Stuff in "quotation marks"')); // Stuff in "quotation marks"
console.log(convertHTML("abc")); // abc
console.log(convertHTML("Dolce & Gabbana")); // Dolce & Gabbana
console.log(convertHTML("<>")); // <>
// ================== Sum All Odd Fibonacci Numbers ===================
/* Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5. */
// 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 etc.
// 1st successful
function sumFibs(num) {
let sum = 0;
let prev = 0;
let next = 1;
let nextNum = 1;
while (nextNum <= num) {
if (nextNum % 2 !== 0) {
sum += nextNum;
}
nextNum = prev + next;
if (nextNum > num) {
return sum;
}
prev = next;
next = nextNum;
}
}
// 2nd, ok. Used a while loop with arr.reduce() for this
function sumFibs(num) {
let prev = 0, next = 1, nextNumSum = 1;
let arr = [];
while (nextNumSum <= num) {
arr.push(nextNumSum);
nextNumSum = prev + next;
prev = next;
next = nextNumSum;
}
return arr.reduce((sum, n) => n % 2 !== 0 ? sum + n : sum + 0, 0);
}
console.log(sumFibs(10)); // 10
console.log(sumFibs(20)); // 23
//from fCC forum forkerino, a ternary recursive solution
const sumFibs = (n, prev = 0, curr = 1, sum = 0) => curr > n ? sum : sumFibs(n, curr, prev + curr, sum + (curr % 2 && curr));
// from fCC forum johnlreavis, using a nested recursive function with a while loop
/* The input ‘b’ is the number in the fibonacci sequence. Start calling the recursive function with 0 and 1, to get 1,1,2…
Otherwise counter and while loop are the same as basic solution.*/
function sumFibs(num) {
let oddsum = 0;
function fib(a, b) {
while (b <= num) {
if (b % 2 === 1) {
oddsum += b;
}
return fib(b, a + b);
}
}
fib(0, 1);
return oddsum;
}
console.log(sumFibs(14)); // 23
console.log(sumFibs(21)); // 44
// ================== Sum All Primes ===================
/* A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.
Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num. */
function sumPrimes(num) {
const primes = [2];
for (let n = 3; n <= num; n += 2) {
if (primes.every((prime) => n % prime != 0)) {
primes.push(n);
}
}
console.log(primes);
return primes.reduce((sum, num) => sum + num, 0);
}
console.log(sumPrimes(30));
/* The guts of the above solution is this from StackOverflow.com "weston" answered April 14, 2015
How to find prime numbers between 0 - 100?
A number is a prime if it is not divisible by other primes lower than the number in question.
1. This builds up a primes array.
2. Tests each new odd candidate n for division against existing found primes lower than n.
3. As an optimization it does not consider even numbers and prepends 2 as a final step (prepending unnecessary, just initialize the "primes" array with 2 in it). */
var primes = [];
for (var n = 3; n <= 100; n += 2) {
if (primes.every(function (prime) { return n % prime != 0 })) {
primes.push(n);
}
}
primes.unshift(2);
// fCC, supposedly faster for large-ish numbers when compared to my solution type, because this one doesn't keep/add-on-to/read an array
function sumPrimes(num) {
// Helper function to check primality
function isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
// Check all numbers for primality
// let sum = 0;
// for (let i = 2; i <= num; i++) {
// if (isPrime(i))
// sum += i;
// }
// My revised version of the section above, @ 1/2 as many iterations
let sum = 2;
for (let i = 3; i <= num; i += 2) {
if (isPrime(i))
sum += i;
}
return sum;
}
console.log(sumPrimes(30));
// ================== Steamroller ===================
// Flatten a nested array. You must account for varying levels of nesting.
// Several very basic options of my own which assumes not more than one array per item
function steamrollArray(arr) {
console.log(arr);
console.log(...arr);
for (let i = 0; i < arr.length; i++) {
arr = [].concat(...arr);
}
return arr;
}
function steamrollArray(arr) {
for (let item of arr) {
arr = [].concat(...arr);
}
return arr;
}
// Final
function steamrollArray(arr) {
arr.forEach(item => arr = [].concat(...arr));
return arr;
}
// fCC, a ternary recursive solution using concat and the spread operator
/* Code Explanation
1. Use spread operator to concatenate each element of arr with an empty array
2. Use Array.some() method to find out if the new array contains an array still
3. If it does, use recursion to call steamrollArray again, passing in the new array to repeat the process on the arrays that were deeply nested
4. If it does not, return the flattened array */
function steamrollArray(arr) {
const flat = [].concat(...arr);
return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
}
console.log(steamrollArray([1, {}, [3, [[4]]]]));
console.log(steamrollArray([1, [2], [3, [[4]]]]));
// ================== Smallest Common Multiple ===================
// Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters. The range will be an array of two numbers that will not necessarily be in numerical order.
// 1st successful, good but collects the range in an array (prob bad for large numbers)
function smallestCommons(arr) {
arr.sort((a, b) => a - b); // Sort the array in ascending numerical order
let big = arr[arr.length - 1]; // big is equal to the biggest integer in the array
let n = arr[1] - 1; // n is equal to the next smaller integer to splice into the array
while (n > arr[0]) { // while n is bigger than the smallest number in the array...
arr.splice(1, 0, n); // splice it in and...
n--; // decrement it by one until the array is full
}
while (arr.every(num => big % num == 0) == false) { // while every num in arr is NOT evenly divisible by big...
big += arr[arr.length - 1]; // increment big by its initial value
}
return big; // after the while loop returns true, return the final big
}
// 2nd, better
// All multiples of a consecutive number range are always even so...
function smallestCommons(arr) {
arr.sort((a, b) => a - b); // Sort the array in ascending numerical order
let big = arr[1], small = arr[0]; // big and small are equal to their respective integers in the array
let multiple = big; // multiple is initialized with the value of big
let incr = 0; // Increment counter
for (let i = big; i >= small; i--) { // for each number in range...
incr++;
if (multiple % i !== 0) { // if multiple modulus of i is not even...
multiple += big; // increment multiple by its initial value and...
i = big; // restart the for loop and counter
}
if (multiple % 2 === 1) { // then if multiple is an odd number...
multiple += big; // increment again (now an even number)
}
}
console.log(`increment = ${incr}`);
return multiple; // after the while loop returns true, return the final big
}
// from fCC forum Jan Egbers (I added the increment counter)
function smallestCommons(arr) {
var max = Math.max(arr[0], arr[1]);
var min = Math.min(arr[0], arr[1]);
var mltple = max;
let incr = 0; // Increment counter
for (var i = max; i >= min; i--) {
incr++; // counter plus 1
if (mltple % i !== 0) {
mltple += max;
i = max;
}
}
console.log(`increment = ${incr}`);
return mltple;
}
// from fCC forum forkerino. I don't fully understand this yet
function smallestCommons(arr) {
const min = Math.min(arr[0], arr[1]),
max = Math.max(arr[0], arr[1]),
range = [...Array(max + 1).keys()].slice(min); // Uses the spread operator, Array constructor, array.keys() and .slice() to make an array of the number range
return range.reduce(function (a, b) {
for (let i = a; i <= a * b; i += a) {
if (i % b === 0) return i;
}
});
}
console.log(smallestCommons([1, 5])) // 60
console.log(smallestCommons([7, 5])); // 210
console.log(smallestCommons([2, 10])); // 2520
console.log(smallestCommons([1, 13])); // 360360
// ================== Drop it ===================
// Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function func returns true when the iterated element is passed through it. Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array.
// 1st successful
function dropElements(arr, func) {
for (let num of arr) {
console.log(`num = ${num}`, `index = ${arr.indexOf(num)}`, func(num));
if (func(num) === true) {
return arr.slice(arr.indexOf(num));
}
}
return [];
}
// fCC (I compacted it)
function dropElements(arr, func) {
while (arr.length > 0 && !func(arr[0])) arr.shift();
return arr;
}
// fCC, a ternary operator slice solution using .findIndex()
/* Code Explanation
1. Use ES6 findIndex() function to find the index of the element that passes the function condition
2. Slice the array from the found index until the end
3. If the condition is not met against any of the elements ‘findIndex’ will return -1 which messes up the input to slice(). So in this case we use a conditional operator which returns false instead of -1. The ternary operator returns the found index of required elements when the condition is true, and the length of the array otherwise so that the return value is an empty array. */
function dropElements(arr, func) {
let sliceIndex = arr.findIndex(func);
return arr.slice(sliceIndex >= 0 ? sliceIndex : arr.length);
}
// from fCC forum okanatabag
function dropElements(arr, func) {
return arr.slice(arr.find(func) ? arr.indexOf(arr.find(func)) : arr.length);
}
// from fCC forum BMars
function dropElements(arr, func) {
var index = arr.map(func).indexOf(true);
console.log(index);
return (index === -1 ? [] : arr.slice(index));
}
// from fCC forum lbarjak, Wow, a little deep yet
dropElements = (arr, func, s = false) => arr.filter(a => { if (func(a)) s = true; return s; });
console.log(dropElements([1, 2, 3, 4], function (n) { return n > 5; })) // []
console.log(dropElements([1, 2, 3, 4], function (n) { return n >= 3; })); // [3, 4]
console.log(dropElements([1, 2, 3], function (n) { return n < 3; })); // [1, 2, 3]
console.log(dropElements([0, 1, 0, 1], function (n) { return n === 1; })); // [1, 0, 1]
// ================== Binary Agents ===================
// Return an English translated sentence of the passed binary string. The binary string will already be space separated.
// 1st successful
function binaryAgent(str) {
let binArr = str.split(' ');
// console.log(binArr);
let english = [];
// console.log(parseInt(strArr[0], 2));
// console.log(String.fromCharCode(bin));
for (let bin of binArr) {
english.push(String.fromCharCode(parseInt(bin, 2))); // A spread array can be passed into fromCharCode but not into parseInt
}
return english.join('');
}
// 2nd, much better
/* Code explanation
1. Split the string into an array at each space
2. Then map each binary
a. First turn it to ascii code with the string constructor fromCharCode()...
b. Using parseInt() with the first param of the binary number and the optional second argument for radix (base of the number) of 2 (for converting from binary to decimal)
c. All thats left is to use .join() and then we have our english */
function binaryAgent(str) {
return str.split(' ').map(bin => String.fromCharCode(parseInt(bin, 2))).join('');
}
// from fCC forum dwrz, using regex
function binaryAgent(str) {
return str.replace(/[0-9]\w+\s|[0-9]\w+\S/g, function (b) {
return String.fromCharCode(parseInt(b, 2));
});
}
// from fCC forum keissar3, using regex (I slightly refactored)
function binaryAgent(str) {
function binto(num) {
return String.fromCharCode(parseInt(num, 2));
}
return str.replace(/\d{8}\s?/g, binto);
}
console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")); // Aren't bonfires fun!?
// ================== Everything Be True ===================
/* Check if the predicate (second argument) is truthy on all elements of a collection (first argument). In other words, you are given an array collection of objects. The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false. */
// 1st successful
function truthCheck(collection, pre) {
return collection.every(obj => obj[pre]); // If it ISN'T 'falsy', then it IS truthy and .every() will return the boolean accordingly
}
// 2nd, minimized
const truthCheck = (collection, pre) => collection.every(obj => obj[pre]);
console.log(truthCheck([{ name: "Quincy", role: "Founder", isBot: false }, { name: "Naomi", role: "", isBot: false }, { name: "Camperbot", role: "Bot", isBot: true }], "isBot")); // false
console.log(truthCheck([{ name: "Quincy", role: "Founder", isBot: false }, { name: "Naomi", role: "", isBot: false }, { name: "Camperbot", role: "Bot", isBot: true }], "name")); // true
console.log(truthCheck([{ name: "Quincy", role: "Founder", isBot: false }, { name: "Naomi", role: "", isBot: false }, { name: "Camperbot", role: "Bot", isBot: true }], "role")); // false
console.log(truthCheck([{ name: "Pikachu", number: 25, caught: 3 }, { name: "Togepi", number: 175, caught: 1 }], "number")); // true
console.log(truthCheck([{ name: "Pikachu", number: 25, caught: 3 }, { name: "Togepi", number: 175, caught: 1 }, { name: "MissingNo", number: NaN, caught: 0 }], "caught")); // false
// ================== Arguments Optional ===================
// Create a function that sums two arguments together. If only one argument is provided at first, then return a function that expects one argument and returns the sum.
// 1st successful
function addTogether(...args) {
let sum = 0;
function sumItUp(args) {
if (args.every(num => typeof num == 'number')) {
for (let para of args) {
sum += para;
}
return sum;
} else return undefined;
}
if (args.length == 1) {
if (typeof args[0] !== 'number') return undefined; // allows for initial input error
return function addTwoNums(param) {
args.push(param);
return sumItUp(args);
}
} if (args.length == 2) return sumItUp(args);
}
// 2nd, better
function addTogether(...args) {
const sumItUp = (args) => args.reduce((sum, para) => args.every(num => typeof num == 'number') ? sum + para : undefined);
if (args.length == 1) {
if (typeof args[0] !== 'number') return undefined; // allows for initial input error
return function addTwoNums(param) {
args.push(param);
return sumItUp(args);
}
} if (args.length == 2) return sumItUp(args);
}
// from fCC forum TheMightyPenguin, a recursive solution, but max of 2 arguments
function addTogether() {
const [a, b] = arguments;
if (typeof a !== 'number' || (b && typeof b !== 'number')) {
return undefined;
}
if (b) {
return a + b;
}
return c => addTogether(a, c);
}
// from fCC forum wiseman, similar to the one above, recursive, but max of 2 arguments
function addTogether(a, b) {
if (![...arguments].every(arg => typeof arg === "number")) {
return
}
if (b) {
return a + b
} else {
return function (y, x = a) {
return addTogether(y, x)
}
}
}
console.log(addTogether(3)(5)(7)); // 15
console.log(addTogether(2, "3")); // undefined
console.log(addTogether(2, 3)); // 5
console.log(addTogether(5)(7)); // 12
console.log(addTogether(2)([3])); // undefined
console.log(addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ")); // undefined
// ================== Make a Person ===================
/* Fill in the object constructor with the following methods below:
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast) */
const Person = function (firstAndLast) {
// Only change code below this line
let nameArr = firstAndLast.split(' ');
let firstName = nameArr[0];
let lastName = nameArr[1];
this.getFirstName = () => firstName;
this.getLastName = () => lastName;
this.getFullName = () => `${firstName} ${lastName}`;
this.setFirstName = (first) => firstName = first;
this.setLastName = (last) => lastName = last;
this.setFullName = (firstAndLast) => {
nameArr = firstAndLast.split(' ');
firstName = nameArr[0];
lastName = nameArr[1];
return `${firstName} ${lastName}`;
}
}
// fCC (I updated, mostly for ES6 arrow function style)
const person = function (firstAndLast) {
let fullName = firstAndLast;
// Getters
this.getFirstName = () => fullName.split(" ")[0];
this.getLastName = () => fullName.split(" ")[1];
this.getFullName = () => fullName;
// Setters
this.setFirstName = (first) => fullName = `${first} ${fullName.split(" ")[1]}`;
this.setLastName = (last) => fullName = `${fullName.split(" ")[0]} ${last}`;
this.setFullName = (firstAndLast) => fullName = firstAndLast;
};
const bob = new person('Bob Ross');
console.log(bob);
console.log(bob.getFullName()); // Bob Ross
console.log(bob.getFirstName()); // Bob
console.log(bob.getLastName()); // Ross
console.log(bob.firstName); // undefined
bob.setFullName("Haskell Curry");
console.log(bob.getFirstName()); // Haskell
bob.setLastName("Curry");
console.log(bob.getFullName()); // Bob Curry
bob.setFirstName("Haskell");
console.log(bob.getFullName()); // Haskell Ross
// ================== Map the Debris ===================
/* Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).
The array will contain objects in the format {name: 'name', avgAlt: avgAlt}.
The values should be rounded to the nearest whole number. The body being orbited is Earth.
The radius of the earth is 6367.4447 kilometers, and the GM value of earth is 398600.4418 km3s-2. */
// Mine
function orbitalPeriod(arr) {
const GM = 398600.4418; // Gravitational mass?
const earthRadius = 6367.4447; // In kilometers
return arr.map((satelite) => ({
name: satelite.name,
orbitalPeriod: Math.round((2 * Math.PI) * Math.sqrt((satelite.avgAlt + earthRadius) ** 3 / GM)) // Calculates the time in seconds. '** 3' is the same as Math.pow(num, 3)
}));
}
// fCC, which uses some variables inside of .map()
function orbitalPeriod(arr) {
const GM = 398600.4418;
const earthRadius = 6367.4447;
return arr.map(({ name, avgAlt }) => {
const earth = earthRadius + avgAlt;
const orbitalPeriod = Math.round(2 * Math.PI * Math.sqrt(Math.pow(earth, 3) / GM));
return { name, orbitalPeriod };
});
}
console.log(orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }])); // [ { name: 'sputnik', orbitalPeriod: 86400 } ]
console.log(orbitalPeriod([{ name: "iss", avgAlt: 413.6 }, { name: "hubble", avgAlt: 556.7 }, { name: "moon", avgAlt: 378632.553 }]));
/* [
{ name: 'iss', orbitalPeriod: 5557 },
{ name: 'hubble', orbitalPeriod: 5734 },
{ name: 'moon', orbitalPeriod: 2377399 }
] */
// The math formula
function calc() {
const GM = 398600.4418; // Earth's GM (the standard gravitational parameter)
const earthRadius = 6367.4447; // in kilometers
let avgAlt = 35873.5553; // Sputnik satellite altitude above the earth
let a = earthRadius + avgAlt; // 'a' is the orbit's semi-major axis
// Kepler's Third Law: (orbital period:)T = 2 * pi * the square root of ('a' to the 3rd power / GM)
let time = Math.round(2 * Math.PI * Math.sqrt(a ** 3 / GM));
return time; // in seconds
}
console.log(calc());