-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgo.js
More file actions
1219 lines (953 loc) · 39.4 KB
/
algo.js
File metadata and controls
1219 lines (953 loc) · 39.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
// Intermediate Algorithm Practice Problems from freeCodeCamp
//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 max = Math.max(...arr);
let min = Math.min(...arr);
let result = 0;
for(let i = min; i <= max; i++) {
result += i;
}
return result;
}
// console.log(sumAll([1,4]));
// Diff Two Arrays
/* Compare two arrays and return a new array with any items found in one of the two
given arrays, but not both. In other words, return the symmetric difference of the two arrays.
*/
function diffArray(arr1, arr2) {
return arr1.concat(arr2).filter((item,_,arr) => arr.lastIndexOf(item) == arr.indexOf(item));
}
// console.log(diffArray([1,2,3,4], [2,4,6,8]));
// Seek & 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. */
function destroyer(arr) {
let args = Array.from(arguments).slice(1);
return arr.filter((val) => {
return !args.includes(val);
});
}
// console.log(destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan"));
// 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). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
function whatIsInAName(collection, source) {
// We create a variable for the source objects keys
let srcKeys = Object.keys(source);
// filter the collection
return collection.filter((obj) => {
for (let i = 0; i < srcKeys.length; i++) {
if (!obj.hasOwnProperty(srcKeys[i]) ||
obj[srcKeys[i]] !== source[srcKeys[i]]
) {
return false;
}
}
return true;
})
}
/* We filter through the collection array, and use a for loop to loop
through each object in the array. We use an if statement to check if
the object in the collection doesn't have the key and the property
value doesn't match the value in source. We return false if the if
statement is correct, otherwise, we return true.*/
// console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
// Spinal Tap Case
// Convert a string to spinal case. Spinal case is all lowercase words joined by dashes
function spinalCase(str) {
//Split the string at the following conditions: whitespace is encountered, underscore is encoutered, or is followed by an uppercase letter. Join using hyphen. Lowercase the resulting string.
return str.split(/\s|_|(?=[A-Z])/).join('-').toLowerCase();
}
// console.log(spinalCase("This is a sentence to test spinal case"));
// Pig Latin
/*Pig Latin is a way of altering English Words. The rules are as follows:
- 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.
- If a word begins with a vowel, just add "way" at the end.
Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase. */
function translatePigLatin(str) {
let vowelRegex = /^[aeiou]/;
let conRegex = /[aeiou]/;
let index = str.search(conRegex);
let conStart = str.substring(0,index);
let vowelEnd = str.substring(index);
if(vowelRegex.test(str)){
return str.concat("way")
} else {
return vowelEnd + conStart + "ay";
}
}
/* First, we create regexs to identify strings that start with a vowel,
and to find the first vowel in a string. Then we create a variable
(index) that will store the index of a strings first vowel. Using the
substring method, we extract the consonants at the start of a string
and store it in a new variable (conStart). Similarly, we create a
variable to store the first vowel and all subsequent characters. We
then run the str through an if/else statement. If the str starts with a
vowel, we return the string and concat "way". Else, we take the
vowelEnd characters and concat conStart and "ay". */
// console.log(translatePigLatin("mythical"));
// Search and Replace
/* Perform a search and replace on the sentence using the arguments provided and return the new sentence.
-First argument is the sentence to perform the search and replace on.
-Second argument is the word that you will be replacing (before).
-Third argument is what you will be replacing the second argument with (after).
-Note: Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog"
*/
function myReplace(str, before, after) {
const replaceRegex = new RegExp(`${before}`);
const caseRegex = /^[A-Z]/;
let upperAfter = after.charAt(0).toUpperCase() + after.slice(1);
let lowerAfter = after.charAt(0).toLowerCase() + after.slice(1)
if(caseRegex.test(before)) {
return str.replace(replaceRegex, upperAfter);
} else {
after[0].toLowerCase();
return str.replace(replaceRegex, lowerAfter);
}
}
/*We start by creating two regular expressions. The first one is created
with a RegExp() constructor, so that the before argument can be
dynamically added. The secod regex checks for if a string starts with
an uppercase letter. We create two vars (upperAfter, lowerAfter) to
mutate the after argument. One changes the first character to
uppercase, the other to lowercase. We use slice() to grab every
character after the first one and concat it to the firs character.
Then we use an if statement to test if before starts with a
capital letter. If it does, we use the replaceRegex to search
the str and replace it with upperAfter. otherwise we replace it
with lowerAfter
*/
// console.log(myReplace("He is Sleeping on the couch", "Sleeping", "sitting"));
// 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 are paired up in an array, and all the arrays are grouped into one encapsulating array.
*/
function pairElement(str) {
let strArray = str.split('');
let arr = [];
for (let i = 0; i < strArray.length; i++ ){
switch(strArray[i]) {
case "A":
arr.push(["A", "T"]);
break;
case "T":
arr.push(["T", "A"]);
break;
case "G":
arr.push(["G", "C"]);
break;
case "C":
arr.push(["C", "G"]);
break
}
return arr;
}
}
/*We turn the str into an array and store it in a new variable(strArray). Then we create an empty array variable (arr) to be modified and
returned. We use a for loop to loop through each index of strArray.
Using a switch statement, we push the base pair array that corresponds
to each case. We return arr at the end, which now holds all the base
pairs for the initial str. */
// console.log(pairElement("ATCGA"));
// 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.
*/
function uniteUnique(arr) {
let finalArr = [];
for(let i = 0; i < arguments.length; i++) {
let arrArgs = arguments[i];
for(let j = 0; j < arrArgs.length; j++){
let indexVal = arrArgs[j];
if(finalArr.indexOf(indexVal) < 0) {
finalArr.push(indexVal);
}
}
}
return finalArr;
}
/* We create empty array finalArr to store the final result.
Loop through the arguments object in the outer loop and store it in arrArgs.The inner loop is used to loop through individual array elements.
If the element doesn’t already exist in finalArr, we add it.
Return finalArr.
*/
// console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));
// Convert HTML Entities
/*Convert the characters &, <, >, " (double quote), and ' (apostrophe),
in a string to their corresponding HTML entities.*/
function convertHTML(str) {
let arrayStr = str.split('');
for(let i = 0; i < arrayStr.length; i++){
switch(arrayStr[i]) {
case "&":
arrayStr.splice(i, 1, "&");
break;
case "<":
arrayStr.splice(i, 1, "<");
break;
case ">":
arrayStr.splice(i, 1, ">");
break;
case `"`:
arrayStr.splice(i, 1, """);
break;
case "'":
arrayStr.splice(i, 1, "'");
break;
}
}
return arrayStr.join('');
}
/*Assign arrayStr to str.split(''), which creates an array containing each individual character in the passed string.
Pass each character in the newly created array into a switch() statement.
Replace the HTML entities with their corresponding HTML entity string (i.e. '&' becomes '&' in line 51)
temp.join('') converts the array of characters into a string to be returned.
*/
// console.log(convertHTML("Sixty > twelve"));
// 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.*/
function sumFibs(num) {
let prevNumber = 0;
let currNumber = 1;
let result = 0;
while (currNumber <= num) {
if (currNumber % 2 !==0) {
result += currNumber;
}
currNumber +=prevNumber;
prevNumber = currNumber - prevNumber;
}
return result;
}
// console.log(18);
/*We create a variable to keep record of the current and previous numbers along with the result that will be returned.
We use a while loop to make sure we do not go over the number given as parameter.
We use the modulo operand to check if the current number is odd or even. If it is odd, add it to the result.
We complete the Fibonacci circle by rotating getting the next number and swapping values after.
Return the result.
*/
// 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) {
let i = 1;
let sum = 0;
while (i <= num) {
if (isPrime(i)) {
sum += i;
}
i++;
}
return sum;
}
//function to check if a number is prime or not
function isPrime(x) {
for (let i = 2; i < x; i++) {
if (x % i === 0) return false;
}
return x !== 1 && x !== 0;
}
/*Create a function to check if a number is prime or not.
Declare two variables. One to keep us within the limit of the given
number and the other to store the sum of numbers to be returned.
Create a loop to check all numbers lesser than or equal to the given
number.
Check if a number is prime and add it to the value of sum.
Return the value of sum once the loop exits.*/
// console.log(sumPrimes(10));
// 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.
*/
function dropElements(arr, func) {
let times = arr.length;
for(let i = 0; i < times; i++) {
if(func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}
/*Create a for loop to check each element.
Then check for the function given if true then stop, otherwise remove
that element.
return the array.*/
// console.log(dropElements([1, 2, 3], function(n) {return n > 2 }));
// Alternate Drop it Algo.
// I did not come up with it, but I like it better than mine
function dropElements(arr, func) {
while (arr.length > 0 && !func(arr[0])) {
arr.shift();
}
return arr;
}
// test here
// console.log(dropElements([1, 2, 3], function(n) {return n > 2 }));
// Steamroller
// Flatten a nested array. You must account for varying levels of nesting. Do not use flat() or flatMap()
function steamrollArray(arr) {
let result = [];
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr.length; i++) {
result.push(arr[i][j])
}
}
return result;
}
// console.log(steamrollArray([1, [2], [3, [[4]]]]));
// Missing Letters
/* Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined. */
function fearNotLetter(str) {
for(let i = 1; i < str.length; i++) {
if( str.charCodeAt(i) - str.charCodeAt(i-1) > 1) {
return String.fromCharCode(str.charCodeAt(i-1) + 1);
}
}
}
/* We begin by looping over the string. We check if the difference in char codes between adjacent characters in the string is more than 1 (check ASCII table). We return the missing character (+1 from where the gap was detected) */
// console.log(fearNotLetter("abce"));
// 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.*/
function truthCheck(collection, pre) {
for(let i = 0; i < collection.length; i++) {
if (!collection[i][pre]) {
return false;
}
}
return true;
};
/* We use a for loop to loop through each object in the collection argument. Using an if statement, we check each index, and if it is not true that it contains pre we return false. Otherwise, we return true. */
// console.log(truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age"));
// Arguments Optional
/*Create a function that sums two arguments together. If only one
argument is provided, then return a function that expects one argument
and returns the sum. For example, addTogether(2, 3) should return 5, and
addTogether(2) should return a function. Calling this returned function
with a single argument will then return the sum:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3) returns 5.
If either argument isn't a valid number, return undefined. */
function addTogether() {
var a = arguments[0];
var b = arguments[1];
function isNum(num) {
return Number.isInteger(num);
}
if(isNum(a)){
if(isNum(b))
return a + b;
else if(!b)
return function(b) {
if (isNum(b))
return a + b;
}
}
}
/* We start by creating variables to hold the first two arguments that
are passed through. Then we create a function (isNum) that checks if an
input is an integer. With an if statement, we check if the first
argument is a number, using our isNum function. If it is, we check if
the second argument is a number. If they both are, we return the sum of
the first and second arguments. Else ff there is only one argument, we
reutrn a function.*/
// console.log(addTogether(2,3));
// Make a Person
/*Fill in the object constructor with the following methods below:
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)
Run the tests to see the expected output for each method. The methods
that take an argument must accept only one argument and it has to be a
string. These methods must be the only available means of interacting
with the object. */
var Person = function(firstAndLast) {
// Only change code below this line
// Complete the method below and implement the others similarly
var fullName = firstAndLast;
this.getFirstName = function() {
return fullName.split(" ")[0];
};
this.getLastName = function() {
return fullName.split(" ")[1];
};
this.getFullName = function() {
return fullName;
};
this.setFirstName = function(name) {
fullName = name + " " + fullName.split(" ")[1];
};
this.setLastName = function(name) {
fullName = fullName.split(" ")[0] + " " + name;
};
this.setFullName = function(name) {
fullName = name;
};
};
// var bob = new Person('Bob Ross');
// bob.getFullName();
//Stop gninnipS My sdroW!
/*Write a function that takes in a string of one or more words, and
returns the same string, but with all five or more letter words reversed
(Just like the name of this Kata). Strings passed in will consist of
only letters and spaces. Spaces will be included only when more than one
word is present.
Examples: spinWords( "Hey fellow warriors" ) => returns "Hey wollef
sroirraw" spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test" */
function spinWords(str){
let arrWords = str.split(' ');
let result = [];
for(let i = 0; i < arrWords.length; i++){
if(arrWords[i].length >= 5) {
let reversed =arrWords[i].split('').reverse().join('');
result.push(reversed)
} else {
result.push(arrWords[i])
}
}
return result.join(' ');
}
// console.log(spinWords('Hey fellow warriors'));
// Quarter of the Year
/* Given a month as an integer from 1 to 12, return to which quarter of
the year it belongs as an integer number.
For example: month 2 (February), is part of the first quarter; month 6
(June), is part of the second quarter; and month 11 (November), is part of
the fourth quarter. */
// const quarterOf = (month) => {
// if (month <= 3) {
// return 1
// } else if (month <= 6) {
// return 2
// } else if (month <= 9) {
// return 3
// } else if (month <= 12) {
// return 4
// }
// }
/* We use if/else if statement to define the range of the months for
each quarter of the year. We return the quarter (1-4) for the month that
is passed through */
// Better Solution Found on CodeWars
const quarterOf =m=> Math.ceil(m / 3);
// console.log(quarterOf(7));
/*Create a function that takes 2 positive integers in form of a string as an input, and outputs the sum (also as a string):
sumStr("4", "5") // should output "9"
sumStr("34", "5") // should output "39"
If either input is an empty string, consider it as zero. */
function sumStr(a,b) {
return String(Number(a)+Number(b));
}
// console.log(parseInt('8', ''));
/*Create a function that converts US dollars (USD) to Chinese Yuan (CNY)
. The input is the amount of USD as an integer, and the output should be
a string that states the amount of Yuan followed by 'Chinese Yuan'
For Example:
usdcny(15) // => '101.25 Chinese Yuan'
usdcny(465) // => '3138.75 Chinese Yuan'
The conversion rate you should use is 6.75 CNY for every 1 USD. All
numbers shold be rounded to the nearest hundredth (e.g. 21.00 NOT 21.0
or 21) */
function usdcny(usd) {
return (usd * 6.75).toFixed(2).concat(' Chinese Yuan');
}
/* We multiply the usd by the provided conversionrate, and use the
toFixed() method so that the output returns to the nearest hundredth.
Then we concat the string. */
// console.log(typeof usdcny(465));
// Multiples of 3 or 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.
Finish the solution so that it returns the sum of all the multiples of 3
or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once.
Also, if a number is negative, return 0(for languages that do have them/*/
function solution(number){
let arr = [];
let result = [0];
for(let i = 1; i < number; i++){
arr.push(i);
} if(number <= 0) {
arr.push(0);
}
for(let i = 0; i < arr.length; i++){
if((Number.isInteger(arr[i] / 3)) || (Number.isInteger(arr[i] / 5)) ) {
result.push(arr[i]);
}
}
return result.reduce((accumulator, currentValue) => accumulator + currentValue);
}
/*We start by creating an empty array, which we will fill with all the
numbers betwee one and number. We also create a result array with an
initial value of 0 (The initial value prevents us from returning an
error if no value is passed through when the function is called). Next
we use a for loop to add every number between 1 and number to the arr
array. If the number is less than or equal to 0, we push 0 to arr. Then
we use another for loop to check each arr value to see if it is a
multiple of 3 or 5. If it is, we push it to our result array. Finally,
we use the reduce method on the result array to sum all the values and
return the sum.
*/
// console.log(solution(25));
// Convert String to Camel Case
/*Complete the method/function so that it converts dash/underscore
delimited words into camel casing. The first word within the output
should be capitalized only if the original word was capitalized
(known as Upper Camel Case, also often referred to as Pascal case). */
function toCamelCase(str){
let replaceStr = str.replace(/[^a-zA-Z0-9]+/g, ' ').toLowerCase().split(' ');
let firstChar = str.slice(0,1);
let result = replaceStr.map((val) => {
return val.replace(val.charAt(0), val.charAt(0).toUpperCase())
})
let finalStr = result.join('');
return finalStr.replace(/[a-zA-Z]/, firstChar);
}
// console.log(toCamelCase('the-Stealth-Warrior'));
// Friend or foe?
/*Make a program that filters a list of strings and returns a list
with only your friends name in it.
If a name has exactly 4 letters in it, you can be sure that it has
to be a friend of yours! Otherwise, you can be sure he's not...
Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"] */
function friend(friends){
let friendArr = [];
for(let i = 0; i < friends.length; i++){
if(friends[i].length === 4){
friendArr.push(friends[i]);
}
}
return friendArr;
}
/* We start by creating an empty array (friendArr), which will be
used to hold the names of friends. Then we use a for loop to
navigate through the passed through array. Any value with the
exact length of 4 is pushed to the friendArr. We then return the
friendArr. */
// console.log(friend(["Ryan", "Kieran", "Jason", "Yous"]));
// Find the Odd Int
/*Given an array of integers, find the one that appears an odd
number of times.
There will always be only one integer that appears an odd number
of times. */
function findOdd(A) {
return A.reduce((a, b) => a ^ b)
}
// Did not write this algorithm. Nead to read up on xor.
// console.log(findOdd([20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]));
// 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. */
function orbitalPeriod(arr) {
let GM = 398600.4418;
let earthRadius = 6367.4447;
return arr.map(obj => {
return {
name: obj.name,
orbitalPeriod: Math.round(2 * Math.PI * Math.sqrt(Math.pow((obj.avgAlt + earthRadius), 3) / GM))
}
});
}
// console.log(orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]));
// Writing a number guessing game
// let maximum = parseInt(prompt('Enter the maximum number!'));
// while(!maximum) {
// maximum = parseInt(prompt('Enter a valid number!'));
// }
// const targetNum = Math.floor(Math.random() * maximum) + 1;
// let guess = prompt('Enter your first guess');
// let attempts = 1;
// while(parseInt(guess) !== targetNum){
// if(guess === 'q') break;
// attempts++;
// if(guess > targetNum){
// guess = prompt('Too high! Enter a new guess.')
// } else {
// guess = prompt('Too low! Enter a new guess.')
// }
// if(guess === 'q'){
// console.log('Ok, you are quitting')
// } else {
// console.log(`You got it! It took you ${attempts} attempts.`);
// }
// Does my number look big in this?
/*A Narcissistic Number is a positive number which is the sum of its own
digits, each raised to the power of the number of digits in a given base.
In this Kata, we will restrict ourselves to decimal (base 10).
For example, take 153 (3 digits), which is narcisstic:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
and 1652 (4 digits), which isn't:
1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938
The Challenge:
Your code must return true or false depending upon whether the given
number is a Narcissistic number in base 10.
Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function. */
function narcissistic(value) {
let strNumArr = value.toString().split('');
let sum = 0;
for(let num of strNumArr){
sum += Number(num) ** strNumArr.length;
}
if(sum === value){
return true;
}
return false;
}
/* We start by converting the number passed in to a string, which we then split into an array.
This allows us to seperate each digit of the number. We then instantiate a sum variable, which
will hold the sum of our for of loop. We then use a for of statement to loop through each number
from our array. Using the Number method, we convert each digit back to a number, which we raise
to the power of the arrays length (which is the number of digits in the original number). We add
this to the sum. Finally, if the final sum equals the initial value, we return true. Otherwise,
we return false.
*/
// console.log(narcissistic(153));
// IQ Test
/* Bob is preparing to pass IQ test. The most frequent task in this test is to find out which
one of the given numbers differs from the others. Bob observed that one number usually differs
from the others in evenness. Help Bob — to check his answers, he needs a program that among the
given numbers finds one that is different in evenness, and return a position of this number.
! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the
elements start from 1 (not 0) */
function iqTest(numbers){
let numArr = numbers.split(' ');
const isEven = numArr.filter(number => number % 2 === 0);
const isOdd = numArr.filter(number => number % 2 !== 0);
let diffNum = 0;
if(isEven.length < isOdd.length){
diffNum = Number(isEven);
} else {
diffNum = Number(isOdd);
}
return numArr.indexOf(diffNum.toString()) + 1;
}
/* We start by splitting the string of numbers that is passed through, so we can work with them
as an array. Next, we create two variables (isEven and isOdd) In isEven we filter the array into
a new array which only contains the even numbers. We do the opposite in isOdd, creating an array
of odd numbers. Next, we use an if/else statement to compare the length of the filtered arrays.
Since we know there will only be one different value, we cover the value of the shorter array to
a number which is stored in the diffNum variable. Finally, we return the index of the diffNum variable
(converted to a string) and add 1.
*/
// console.log(iqTest("2 4 7 8 10"));
// Bouncing Balls
/* A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known.
He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66).
His mother looks out of a window 1.5 meters from the ground.
How many times will the mother see the ball pass in front of her window (including when it's falling and bouncing?
Three conditions must be met for a valid experiment:
Float parameter "h" in meters must be greater than 0
Float parameter "bounce" must be greater than 0 and less than 1
Float parameter "window" must be less than h.
If all three conditions above are fulfilled, return a positive integer, otherwise return -1.
Note:
The ball can only be seen if the height of the rebounding ball is strictly greater than the window parameter.*/
function bouncingBall(h, bounce, window) {
let counter = 0;
let bounceHeight
while(h > window){
h * bounce;
counter++
}
return counter;
}
// Array Diff
/*Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list a, which are present in list b.
arrayDiff([1,2],[1]) == [2]
If a value is present in b, all of its occurrences must be removed from the other:
arrayDiff([1,2,2,2,3],[2]) == [1,3] */
function arrayDiff(a, b) {
let result = [];
for(let i = 0; i < a.length; i++){
if(!b.includes(a[i])){
result.push(a[i])
}
}
return result;
}
// console.log(arrayDiff([], [4,5]));
// console.log(arrayDiff([3,4], [3]));
// console.log(arrayDiff([1,8,2], []));
// 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.
For example, if given 1 and 3, find the smallest common
multiple of both 1 and 3 that is also evenly divisible
by all numbers between 1 and 3. The answer here would
be 6. */
function smallestCommons(arr) {
arr.sort(function(a, b) {
return b - a;
});
let rangeArr =[];
for(let i = arr[0]; i >= arr[1]; i--){
rangeArr.push(i);
}
let quot = 0;
let loop = 1;
let i;
do {
quot = rangeArr[0] * loop * rangeArr[1];
for(i = 2; i < rangeArr.length; i++){
if(quot % rangeArr[i] !==0){
break;
}
}
loop++;
} while(i !== rangeArr.length);
return quot;
}
// console.log(smallestComm([1,5]));
// Steamroller
// Flatten a nested array. You must account for varying levels of nesting.
//Your solution should not use the Array.prototype.flat() or Array.prototype.flatMap() methods.
function steamrollArray(arr) {
function flatDeep(arr, d = 1) {
return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1)
: val), [])
: arr.slice();
};
return flatDeep(arr, Infinity);
}
/* I did not write this algorithm! It was taken from MDN's page on flat()
method. Saving it here to come back to reference.*/
// console.log(steamrollArray([1, [2], [3, [[4]]]]));
// Binary Agents
//Return an English translated sentence of the passed binary string. The binary string will be space separated.
function binaryAgent(str) {
let binaryArr = str.split(' ');
let decimalArr = [];
let wordArr = [];
for(let digit of binaryArr){
decimalArr.push(parseInt(digit, 2))
}
for(let decimal of decimalArr){
wordArr.push(String.fromCharCode(decimal))
}
return wordArr.join('');
}
/* We start by splitting the string into an array. We also create two empty
arrays which will hold values pushed from for/of loops. In the first for/of
loop, we convert the binary value into a decimal value using parseInt. We
then take the array of decimals and convert them to a string from the charCode
at that decimal value. We then join and return the array of characters */
// console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));
// Digital root is the recursive sum of all the digits in a number.
// Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.
function digital_root(n) {
let num = n;
if(num < 10){
return num
} else {
let numArr = [];
let numString = num.toString().split('');