-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgoLadder.js
More file actions
1248 lines (962 loc) · 38.4 KB
/
algoLadder.js
File metadata and controls
1248 lines (962 loc) · 38.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
// Algo 1: Reduce Sum
// Return the sum of all numbers in a given array.
const reduceSum = (arr) => {
return arr.reduce((sum, value) => sum + value, 0);
}
// console.log(reduceSum([1, 2, 3, 4]))
const arrSum = (arr) => {
let sum = 0;
for(let num of arr){
sum += num;
}
return sum;
}
// console.log(arrSum([1, 2, 3, 4]))
// The reduce sum function was fisrt solved using a reduce function and then with a for/of functions
// Algo 2: Less than 100
// Given an array of numbers, return a new array that contains all numbers from the original array
//that are less than 100.
const underOneHundo = (arr) => {
return arr.filter(num => num < 100)
}
// console.log(underOneHundo([99, 101, 88, 4, 2000, 50]))
const underOneHundoLoop = (arr) => {
let result = [];
for(let num of arr){
if(num < 100){
result.push(num)
}
}
return result;
}
// console.log(underOneHundo([99, 101, 88, 4, 2000, 50]))
// In the first solution, we filtered the givern array to create a new array that only contains numbers under 100
// In the second solutions, we used a for/of loop to push the numbers under one hundred into a new array
// Algo 3: Map Double
//Given an array of numbers, return a new array whose values are the original array’s value doubled.
const seeingDouble = (arr) => {
return arr.map((num) => num * 2);
}
// console.log(seeingDouble([4, 2, 5, 99, -4]));
// For this solution we just have to use the map method and multiply each number by 2
//Algo 4: Array Max
// Return the greatest value from an array of numbers.
const toTheMax = (arr) => {
return Math.max(...arr);
}
// console.log(toTheMax([5, 17, -4, 20, 12]))
const getTheMax = (arr) => {
let greatestNum = arr[0];
for(let i = 0; i < arr.length; i++){
if(arr[i] > greatestNum){
greatestNum = arr[i]
}
}
return greatestNum;
}
// console.log(getTheMax([5, 17, -4, 20, 12]))
//In the first solution we use the Math.max method and spread the give array into it to find the max
//In the second solution we use a for loop to determine the greatest number
// Algo 5: Reduce: Product
// Given an array of numbers, return the product of all the numbers.
const product = (arr) => {
return arr.reduce((sum, value) => sum * value)
}
// console.log(product([1, 2, 3, 4]))
//In this solution we simply call the reduce method on the given array and pass it a function that multiplies each value by the accumulator(sum)
//Algo 6: Reverse Array
//Given an array, return a new array that contains the original array’s values in reverse.
const reverse = (arr) => {
return arr.reverse();
}
// console.log(reverse([1, 2, 3, 4, 5]));
const reverseIt = (arr) => {
let reverseArr = [];
for(let num of arr){
reverseArr.unshift(num)
}
return reverseArr;
}
// console.log(reverseIt([1, 2, 3, 4, 5]));
// In the first solution we use the array reverse function method to reverse the entire array
//In the second solution we use unshift method to create a new array with the numbers in reverse order
// Algo 7: Skip It
// Given an array of numbers, return a new array in which only select numbers from the original array are included, based on the following details:
// The new array should always start with the first number from the original array. The next number that should be included depends on what the first number is. The first number dictates how many spaces to the right the computer should move to pick the next number. For example, if the first number is 2, then the next number that the computer should select would be two spaces to the right. This number gets added to the new array. If this next number happens to be a 4, then the next number after that is the one four spaces to the right. And so on and so forth until the end of the array is reached.
const skipIt = (arr) => {
let result = [];
let counter = 0;
for(let i = 0; i < arr.length; i++){
if(i === counter){
result.push(arr[i])
counter += arr[i];
}
}
return result;
}
// console.log(skipIt([2, 1, 3, 2, 5, 1, 2, 6, 2, 7, 1, 5, 6, 3, 2, 6, 2, 1, 2]));
//For this solution we create an empty array to hold the desired numbers. We also set a counter variable to track which numbers to return
//We then use a for loop to iterate over the given array. If the index is equal to the counter, we add the number at that index into our
//result array. We then add that number to the counter. If the index is not equal to the counter we do nothing.
//Algo 8: Reverse String
// Return the reverse of a given string.
const reverseString = (str) => {
return str.split('').reverse().join('');
}
// console.log(reverseString('abcde'));
//For this solution we use the split method to turn the given string into an array. Each character has its own index, so we can reverse the array and
//then join it back together without spaces between the letters
const revString = (str) => {
let newString = "";
for(let i = str.length; i >= 0; i--){
newString += `${str.charAt(i)}`;
}
return newString;
}
// console.log(revString("flip me right round"))
// Algo 9: Show me the money
// Given a string, return true if the “$” character is contained within the string or false if it is not.
const money = (str) => {
return str.includes("$") ? true : false;
}
// console.log(money("i hate but i love money i know i know im crazy"))
// For this solution we use a ternary operator to return true if the string includes a $, using the includes method
//Algo 10: Alternate Capitals
//Given a string, return a copy of the original string that has every other character capitalized. (Capitalization should begin with the second character.)
// const alternator = (str) => {
// let newStr = str.split('');
// let arr = [];
// for(let i = 1; i < newStr.length; i + 2){
// arr.push(newStr[i].toUpperCase())
// }
// return arr;
// }
// // console.log(alternator('hello, how are your porcupines today?”'));
//Algo 11: First Duplicate Character
// Given a string, find the first occurence of two duplicate characters in a row, and return the duplicated character.
const duplicate = (str) => {
let strArr = str.split('');
let noDupes = "There don't appear to be any duplicates"
for(let i = 0; i < strArr.length; i++){
if(strArr[i] === strArr[i+1]){
return strArr[i]
}
}
return noDupes;
}
// console.log(duplicate('abcdefghijklmnopqrstuvwxyz'))
//In this solution we turn the given string into an array. Then we create a variable to be returned if there are no duplicates
//Next, we use a for loop to loop over every character of the array until we find a duplicate. We know a character has a duplicate
//next to it if it is identical to the character at the index which follows it
const foundADupe = (str) => {
for(let i = 0; i < str.length; i++){
if(str.charAt(i) === str.charAt(i+1)){
console.log(str.charAt([i]))
console.log(str.charAt([i++]))
return str[i]
}
}
}
// console.log(foundADupe("This here cool string is a test"))
//Algo 12: Reverse Words
// Given a string of words, return a new string that contains the words in reverse order.
const reverseWords = (str) => {
return str.split(' ').reverse().join(' ')
}
// console.log(reverseWords('popcorn is so cool isn’t it yeah i thought so'))
const reversedWords = (str) => {
let strSplit = str.split(' ');
let result = [];
for(let i = 0; i < strSplit.length; i++){
result.unshift(strSplit[i]);
}
return result.join(' ');
}
// console.log(reversedWords('popcorn is so cool isn’t it yeah i thought so'))
//In the first solution we split the given string into an array, keeping the spacing between words. Then we reverse the array and join it back together.
//In the second solution we also split the given string into an array. We then use a for loop to iterate over the array. We unshift each item into the
//result array to reverse the original order. Finally, we return the result array after turning it into a string.
//Algo 13: Palindrome
//Given a string, return true if it is a palindrome, and false if it is not.
const palindrome = (str) => {
let reversal = str.split('').reverse().join('');
if(reversal === str){
return true;
}
return false;
}
// console.log(palindrome('racecars'));
//In this solution we create a variable which holds the reversed version of the original string.
//We then use an if statement to compare it to the original
const palSyndrome = (str) => {
let pal = ""
for(let i = str.length; i >= 0; i--){
pal += str.charAt(i);
}
if(pal === str){
return true
}
return false
}
console.log(palSyndrome("racecar"))
//Algo 14: Hamming
//Given two strings of equal length, return the number of characters that are different between the two strings.
const hamming = (str1, str2) => {
let arr1 = str1.split('');
let arr2 = str2.split('');
let counter = 0;
for(let i = 0; i < arr1.length; i++){
if(arr1[i] !== arr2[i]){
counter++
}
}
return counter;
}
// console.log(hamming('ABCDEFG', 'ABCXEOG'));
//For this solution we turn both of the given strings into arrays so that we can iterate over them.
//We create a counter variable which will track the number of differencese. We loop over the first array
//comparing its indices to the comparable indices of the second array. For each difference, we add one to the counter
//Algo 15: Primes
//Write a function that returns whether a given number is a prime number.
const primes = (num) => {
if(num <= 1) return false;
if(num === 2) return true;
for(let i = 2; i < num; i++){
if(num % i === 0){
return false
}
}
// return true;
}
// console.log(primes(55));
//For this solution we start by checking if a number is less than or equal to 1, which would mean it is not prime.
//Next we check if the number is equal to 2, since that is the only even prime number.
// Then we use a for loop to check every number between two and the given number. If the given number divided by i is
// zero, then we know it is not a prime number. If it makes it all the way through the for loop we know it is a prime number
// Algo 16: FizzBuzz!
//Write a function that prints out every number from 1 to N, with the following exceptions:
// If the number is divisible by 3, print out "FIZZ".
// If the number is divisible by 5, print out "BUZZ".
// If the number is divisible by both 3 and 5, print out "FIZZBUZZ".
const fizzBuzz = (num) => {
let result = [];
for(let i = 1; i <= num; i++){
if(Number.isInteger(i/3) && Number.isInteger(i/5)){
result.push("FIZZBUZZ");
} else if(Number.isInteger(i/3)){
result.push("FIZZ")
} else if(Number.isInteger(i/5)){
result.push("BUZZ")
} else{
result.push(i);
}
}
return result;
}
// console.log(fizzBuzz(100));
const fizzyBuzzer = (num) => {
let result = []
for(let i = 1; i <= num; i++){
if(i % 3 === 0 && i % 5 === 0){
result.push("FIZZBUZZ")
} else if(i % 3 === 0){
result.push("FIZZ")
} else if(i % 5 === 0){
result.push("BUZZ")
} else{
result.push(i)
}
}
return result;
}
// console.log(fizzyBuzzer(100))
//Algo 17: Leap Year
// Given a year, report if it is a leap year.
// The tricky thing here is that a leap year in the Gregorian calendar occurs:
// on every year that is evenly divisible by 4
// except every year that is evenly divisible by 100
// unless the year is also evenly divisible by 400
// For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.
const leapYear = (num) => {
if(Number.isInteger(num/100) && !Number.isInteger(num/400)){
return false;
} else if(Number.isInteger(num/4)){
return true;
}
return false;
}
// console.log(leapYear(200));
//For this solution we start by checking for the one instance in which a number divisible by four is not a leap year
//Next we check if the number is divisible by four. If it is, then we have ourselves a leap year! Otherwise, we return false.
//Algo 18: Fibonacci Numbers
// Write a function that gives the Nth number of the Fibonacci Sequence.
// The Fibonacci sequence begins with 0 and 1, and every number thereafter is the sum of the previous two numbers.
// So the sequence goes like this:
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on until infinity...
const fibonacci = (num) => {
if(num === 1) return 0;
if(num === 2) return 1;
let arr = [0, 1];
let counter = 1
for(let i = 2; i <= num; i++){
counter = (arr[i-1] + arr[i-2]);
arr.push(counter);
}
return counter;
}
// console.log(fibonacci(100))
//In this solution, we start by returning the first two numbers of the fibonacci sequence, if they are given as an argumetn.
//Next, we create an array which holds the first two numbers of the sequence. Then we create a counter, which begins ar the secong
//number of the sequence. Then we use a for loop to add the two previous fibonacci numbers together, until we have the correlating
//fibonacci number for the argument passed in. We tracke the fibonacci numbers by adding them to our array.
const fibbersNacci = (num) => {
if(num === 1){
return 0;
}
if(num === 2){
return 1
}
let fibArr = [0, 1]
for(let i = 2; i < num; i++){
let sumMe = fibArr[i - 1];
let sumTwo = fibArr[i - 2];
let sum = sumMe + sumTwo;
fibArr.push(sum)
}
return fibArr.pop();
}
// console.log(fibbersNacci(5))
const fibArrNacci = (num) => {
if(num === 1){return 0}
if(num === 2){return 1}
let twoPastNum = 0;
let lastNum = 1;
let sum = 1;
for(let i = 3; i <= num; i++){
sum = (lastNum + twoPastNum)
// console.log(sum)
twoPastNum = lastNum;
lastNum = sum;
}
return sum;
}
// console.log(fibArrNacci(9))
//Algo 19: Multiples of 3 & 5
//If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
// Find the sum of all the multiples of 3 or 5 below 1000.
const multiples = (num) => {
let sum = 0;
for(let i = 0; i < num; i++){
if(Number.isInteger(i/3) || Number.isInteger(i/5)){
sum += i
}
}
return sum;
}
console.log(multiples(1000));
//For this solution we start by creating a variable that will hold the sum of all numbers that are divisble by either 3 or 5
//Next we use a for loop to check every number between 0 and the given number to see if it is divisible by 3 or 5
//If it is divisble then we add it to the sum variable, which we return at the end
const threesAndFives = (num) => {
let sum = 0;
for(let i = 0; i < num; i++){
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}
return sum;
}
// console.log(threesAndFives(10))
//Algo 20: Collatz conjecture
//The Collatz Conjecture or 3x+1 problem can be summarized as follows:
// Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely. The conjecture states that no matter which number you start with, you will always reach 1 eventually.
// Given a number n, return the number of steps required to reach 1.
const collatzConjecture = (num) => {
let steps = 0;
do{
if(num % 2 === 0){
num = num / 2;
steps++
} else {
num = num * 3 + 1;
steps++
}
} while(num > 1);
return steps;
}
// console.log(collatzConjecture(100))
//In this solution we start by creating a variable to keep track of the number of steps
//Next we run a do/while loop and if a number is even we divide it by two and add 1 to steps
//If the number is odd we mulitply it by three and add one and increase steps.
// We keep repeating that until the num is 1 and then we return the number of steps
const collCatz = (num) => {
let steps = 0;
let newNum = num;
do{
console.log(newNum)
if(newNum % 2 === 0){
newNum = newNum / 2;
}else{
newNum = (newNum * 3) + 1;
}
steps++
}while(newNum !== 1)
return steps;
}
// s
// Algo 21: Largest Palindrome Product
// A palindromic number reads the same both ways.
// The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.
const largestPalindrome = () => {
}
//Algo 22: Array Mesh 1
// Given two arrays of strings, return a new string that contains every combination
// of a string from the first array concatenated with a string from the second array.
const arrayMesh = (arr1, arr2) => {
let result = [];
for(let i = 0; i < arr1.length; i++){
for(let j = 0; j < arr2.length; j++){
result.push(arr1[i] + arr2[j])
}
}
return result;
}
// console.log(arrayMesh(["a", "b", "c"], ["d", "e", "f", "g"]));
//In this solution we start by creating an empty array which we will use to store every
//combination of string concatenation. Next we use a for loop to loop over each index of our
//first given array. Inside that for loop we create another for loop to loop over every index
//of the second given array. In this second loop we push every combination of srtings to our result array.
//Finally, we return the result.
const arrMesh = (arr1, arr2) => {
let newArr = [];
for(let i = 0; i < arr1.length; i++){
for(let j =0; j < arr2.length; j++){
newArr.push(arr1[i] + arr2[j])
}
}
return newArr;
}
// console.log(arrMesh(["a", "b", "c"], ["d", "e", "f", "g"]))
//Algo 23: Array Mesh 2
//Given ONE array of strings, return a new array that contains every combination of each string with every other string in the array.
const arrayMeshTwo = (arr) => {
let result = [];
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr.length; j++){
if(arr[i] !== arr[j]){
result.push(arr[i] + arr[j])
}
}
}
return result;
}
// console.log(arrayMeshTwo(["a", "b", "c", "d"]));
//Algo 24: Largest Profuct
//Find the largest product of any two numbers within a given array.
const largestProduct = (arr) => {
let result = [];
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr.length; j++){
if(i < j){
result.push(arr[i] * arr[j])
}
}
}
return result.reduce((prod, value) => {
if(value > prod){
prod = value;
}
return prod;
});
}
// console.log(largestProduct([5, -2, 1, -9, -7, 2, 6]));
//In this solution we start by creating an array to hold all of the products.
//Next, we create a for loop to go over each number in the array. We run another for loop
//inside of the first loop. If the index[i] is less than the index[j] we multiply arr[i] * arr[j].
//This prevents us from multiplying numbers by themselves, and keeps us from duplicating product values
//in our result array. Next we use the reduce method on our result array. Our accumulator is going to
//be our largest product, which we return at the end. Using an if statement we compare the current value to the
//product, and if it is greater we set the largest product equal to value.
//Algo 25: Two Sum 1
//Given an array of numbers, return a new array containing just two numbers (from the original array)
// that add up to the number 10. If there are no two numbers that add up to 10, return false.
// Specifically use nested loops to solve this exercise even though there are other approaches as well.
const twoSumOne = (arr) => {
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr.length; j++){
if(i !== j && arr[i] + arr[j] === 10){
return [arr[i], arr[j]]
}
}
}
return false;
}
// console.log(twoSumOne([2, 5, 3, 1, 0, 7, 11]));
//In this solution, we use a nested for loop to loop through an array. The first loop loops over
//each of the numbers in the given array. Each number is added to every other number in the second
//for loop. We prevent numbers being added to themselves using an if statement to compare the
//indexes. If the two numbers are equal to 10 we return the numbers in an array. If there are
//no numbers which add up to 10, we return false.
//Algo 26: Merge Sorted Arrays
//Given two sorted arrays, merge the second array into the first array while
// ensuring that the first array remains sorted. Do not use any built-in sort methods.
const mergeSorted = (arr1, arr2) => {
for(let i = 0; i < arr2.length; i++){
for(let j = 0; j < arr1.length; j++){
if(arr2[i] > arr1[j] && arr2[i] < arr1[j+1]){
arr1.splice((j + 1), 0, arr2[i])
} else if(arr2[i] > arr1[arr1.length - 1]){
arr1.push(arr2[i])
} else if(arr2[i] < arr1[0]){
arr1.unshift(arr2[i])
}
}
}
return arr1;
}
// console.log(mergeSorted([2, 5, 8], [1, 6, 9]))
//In this solution, we use a nested for loop to loop through the two arrays. The outer
//for loop is for the second array, whose values will be added to the first array. In
//the second for loop we are looping over the first array. Using if statements we compare our
//current arr2 value to the current arr1 value. If the arr2 value is greater than the current arr1
//value and less than the next arr1 value we know that's where we need to insert the arr2
//value. We can use the splice method to insert the arr2 value at the index currently occupied
//by the value that is greater than it. If the arr2 value is greater than all arr1 values
//we can simply push it to the end of arr1. If the arr2 value is less than all arr1 values
//we can simply unshift it to the end arr1.
//Algo 27: 100 Coolio Array
//Given an array of numbers, return true if the array is a "100 Coolio Array", or false if it is not.
// A "100 Coolio Array" meets the following criteria:
// Its first and last numbers add up to 100,
// Its second and second-to-last numbers add up to 100,
// Its third and third-to-last numbers add up to 100,
// and so on and so forth.
const coolio = (arr) => {
let length = arr.length;
let arr1 = arr.slice(0, (length / 2))
let arr2 = arr.slice(length/2).reverse();
for(let i = 0; i < arr1.length; i++){
if(arr1[i] + arr2[i] !== 100){
return false;
}
}
return true;
}
// console.log(coolio([90, 20, 70, 100, 30, 80, 10]))
//In this solution we start by splitting the given array in half using the slice
//method. We reverse the second half of the array to make it easier to loop over
//and add the numbers together at the corresponding index. Using a for loop, we
//add the arr1 and arr2 values at the same index together. If they do not equal
//100 then we return false. Otherwise, at the end of the function, we return true.
//Algo 28: Longest Common Prefix
// Write a function to find the longest common prefix string amongst an array of strings.
// If there is no common prefix, return an empty string "".
// const largestCommonPrefix = (arr) => {
// let longestStr = arr.reduce((str, value) => {
// if(value.length > str.length){
// str = value;
// }
// return str;
// })
// for(let i = 0; i < longestStr.length; i++){
// }
// return longestLength.length;
// }
// console.log(largestCommonPrefix(["flower","flow","flight"]))
//Algo 29: Most Frequent Letter
//Given a string, find the most commonly occurring letter.
// Input: “peter piper picked a peck of pickled peppers”
// Output: “p”
const letterFrequency = (str) => {
let letters = {};
let modStr = str.split(' ').join('');
for(let i = 0; i < modStr.length; i++){
if(!letters.hasOwnProperty(modStr.charAt(i))){
letters[modStr[i]] = 1;
} else {
letters[modStr[i]] += 1;
}
}
let counter = 0;
let result = "";
for(let key in letters){
if(letters[key] > counter){
counter = letters[key];
result = key;
}
}
return result;
}
// console.log(letterFrequency('peter piper picked a peck of pickled peppers'))
//In this solution we start by creating an empty object which we will use to store
//the different letters as the keys and the number of occurences as the value.
//We then modify the string to remove the spaces. Next, we use a for loop to iterate over
//each letter in the string. If our letters obj does not have a property of the current character
//we add the character as a key with the value of 1. If it already has the character as a key,
//we add 1 to the value. Next, we create a counter and results variable which will track the
//character that has the highest number of occurenses. We check the number of occurences using
//a for in loop. If the number of occuences is higher than the counter variable we update the counter
//and the result variable. In the end we return result.
const frequentLetter = (str) => {
let letters = {};
let newStr = str.split(" ").join("").toLowerCase();
for(let char of newStr){
if(letters[char] > 0){
letters[char]++
} else{
letters[char] = 1
}
}
let currentHigh = 0;
let mostCommon = "";
for(let char in letters){
if(letters[char] > currentHigh){
currentHigh = letters[char];
mostCommon = char;
}
}
return mostCommon;
}
// console.log(frequentLetter("peter Piper Picked a peck of pickled peppers"))
//Algo 30: Count Votes
//Given an array of strings, return a hash that provides of a count of how many times each string occurs.
const countVotes = (arr) => {
let votes = {};
for(let i = 0; i < arr.length; i++){
if(!votes.hasOwnProperty(arr[i])){
votes[arr[i]] = 1;
} else{
votes[arr[i]] += 1;
}
}
return votes;
}
// console.log(countVotes(["Dewey", "Truman", "Dewey", "Dewey", "Truman", "Truman", "Dewey", "Truman", "Truman", "Dewey", "Dewey"]))
//In this solution, we start by creating an empty object. Then we loop over the given array.
//If our object does not have a property that corresponds to the given array, we add it to
//the object with an initial value of 1. If it does have that property then we add one
//to the value. Finally, we return the object.
const stopTheCount = (arr) => {
let count = {};
for(let str of arr){
count[str] > 0 ? count[str]++ : count[str] = 1;
}
return count;
}
// console.log(stopTheCount(["Dewey", "Truman", "Dewey", "Dewey", "Truman", "Truman", "Dewey", "Truman", "Truman", "Dewey", "Dewey"]))
//Algo 31: Order the whole menu
// Given a hash, where the keys are strings representing food items,
// and the values are numbers representing the price of each food,
// return the amount someone would pay if they'd order one of each food from the entire menu.
const orderUp = (obj) =>{
let totalPrice = 0;
for(let key in obj){
totalPrice += obj[key];
}
return totalPrice;
}
// console.log(orderUp( {"hot dog": 2, "hamburger": 3, "steak sandwich": 5, "fries": 3, "cole slaw": 1, "soda": 2}))
//In this solution we start by creating a totalPrice variable which will store the total price
//of purchasing every menu item. Next, we use a for in loop to add the value of each key/vaule
//pair to the totalPrice variable. Finally we return the totalPrice.
const bidOrder = (obj) => {
let sum = 0;
for(let item in obj){
sum += obj[item]
}
return sum;
}
//Algo 32: RNA Transcription
// Given a DNA strand, return its RNA complement (per RNA transcription).
// Both DNA and RNA strands are a sequence of nucleotides.
// Here we're representing the sequences with single-letter characters (e.g. a sample strand may look like: "AGCA".)
// Given a string representing a DNA strand, provide its transcribed RNA strand, according to the following pattern:
// G becomes C
// C becomes G
// T becomes A
// A becomes U
const rnaTranscription = (str) => {
const dnaToRna = {
'G': 'C',
'C': 'G',
'T': 'A',
'A': 'U'
}
let rna = [];
for(let i = 0; i < str.length; i++){
rna.push(dnaToRna[str[i]]);
}
return rna.join('');
}
// console.log(rnaTranscription('ACGTGGTCTTAA'))
//In this solution we start by creating a hash that has the DNA sequence as the key
//with the complementary RNA sequence as the value. We create an empty array, in which
//we will store the RNA strand that corresponds to the given DNA. We use a for loop
//to loop over the DNA strand. We push the value of the key in our object that corresponds
//to current DNA character. Finally, we join the array and return the resulting string.
const dnaToRNA = (str) => {
const dna = {"G":"C", "C":"G", "T":"A", "A":"U"};
let rna = "";
for(let char of str){
rna += dna[char]
}
return rna;
}
// console.log(dnaToRNA('ACGTGGTCTTAA'));
//Algo 33: Popular Posts
// Given an array of hashes that represent a list of social media posts,
// return a new array that only contains the posts that have at least 1000 likes.
const popularPosts = (arr) => {
let topPosts = [];
for(let post of arr){
for(let likes in post){
if(post.hasOwnProperty(likes) && post[likes] >= 1000){
topPosts.push(post)
}
}
}
return topPosts;
}
// console.log(popularPosts([
// {title: 'Me Eating Pizza', submitted_by: "Joelle P.", likes: 1549},
// {title: 'i never knew how cool i was until now', submitted_by: "Lyndon Johnson", likes: 3},
// {title: 'best selfie evar!!!', submitted_by: "Patti Q.", likes: 1092},
// {title: 'Mondays are the worst', submitted_by: "Aunty Em", likes: 644}
// ]));
//In this solution we start by creating an empty array, which will hold the most
//popular posts. Next we use a for/of loop to loop over each obj in the given
//array. Inside that we use a for/in loop to loop over the properties of the
//obj. If the object has a property called likes and likes are greater than or equal to 1000
//then we push that post into our topPosts array. Finally we return the topPosts array.
const popularity = (arr) => {
let sorted = [];
for(let obj of arr){
if(obj["likes"] >= 1000){
sorted.push(obj)
}
}
return sorted;
}
// console.log(popularity(([
// {title: 'Me Eating Pizza', submitted_by: "Joelle P.", likes: 1549},
// {title: 'i never knew how cool i was until now', submitted_by: "Lyndon Johnson", likes: 3},
// {title: 'best selfie evar!!!', submitted_by: "Patti Q.", likes: 1092},
// {title: 'Mondays are the worst', submitted_by: "Aunty Em", likes: 644}
// ])))
//Algo 34: Complete the Data
// Given an array of social media posts and a hash of users, return a list of posts
// (as an array of hashes) that replaces the submitted_by id number as the person's actual name.
const completeData = (arr, obj) => {
for(let post of arr){
for(let key in post){
for(let id in obj){
if(post[key] === id){
post[key] = obj[id]
}
}
}