-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
2324 lines (1706 loc) · 61.5 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Kata 1
Description
We need a function that can transform a string into a number. What ways of achieving this do you know?
Note: Don't worry, all inputs will be strings, and every string is a perfectly valid representation of an integral number
Url: https://www.codewars.com/kata/544675c6f971f7399a000e79
*/
const stringToNumber = function (str) {
return Number(str);
};
/*
Kata 2
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.
Additionally, if the number is negative, return 0.
Note: If the number is a multiple of both 3 and 5, only count it once.
Courtesy of projecteuler.net (Problem 1)
Url: https://www.codewars.com/kata/514b92a657cdc65150000006
*/
function solution(number) {
arr = [];
for (let i = 0; i <= number; i++) {
arr.push(i);
}
let sum = 0;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] % 3 == 0 || arr[i] % 5 == 0) {
sum = arr[i] + sum;
}
if (number < 0) return 0;
}
return sum;
}
/*
Kata 3
A square of squares
You like building blocks. You especially like building blocks that are squares. And what you even like more, is to arrange them into a square of square building blocks!
However, sometimes, you can't arrange them into a square. Instead, you end up with an ordinary rectangle! Those blasted things! If you just had a way to know, whether you're currently working in vain… Wait! That's it! You just have to check if your number of building blocks is a perfect square.
Task
Given an integral number, determine if it's a square number:
In mathematics, a square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself.
The tests will always use some integral number, so don't worry about that in dynamic typed languages.
Examples
-1 => false
0 => true
3 => false
4 => true
25 => true
26 => false
Fundamentals
Url: https://www.codewars.com/kata/54c27a33fb7da0db0100040e/
*/
var isSquare = function (n) {
if (Number.isInteger(Math.sqrt(n))) {
return true;
} else return false;
};
/*
Kata 4
Convert a Number to a String!
We need a function that can transform a number (integer) into a string.
What ways of achieving this do you know?
Examples (input --> output):
123 --> "123"
999 --> "999"
-100 --> "-100"
Url: https://www.codewars.com/kata/5265326f5fda8eb1160004c8/
*/
function numberToString(num) {
return String(num);
}
/*
Kata 5
Vowel Count
Return the number (count) of vowels in the given string.
We will consider a, e, i, o, u as vowels for this Kata (but not y).
The input string will only consist of lower case letters and/or spaces.
Url: https://www.codewars.com/kata/54ff3102c1bad923760001f3/
*/
function getCount(str) {
let count = 0;
const vowels = ["a", "e", "i", "o", "u"];
const charArray = str.split("");
for (let i = 0; i < charArray.length; i++) {
if (vowels.includes(charArray[i])) {
count++;
}
}
return count;
}
/*
Kata 6
Get the Middle Character
You are going to be given a word. Your job is to return the middle character of the word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.
#Examples:
Kata.getMiddle("test") should return "es"
Kata.getMiddle("testing") should return "t"
Kata.getMiddle("middle") should return "dd"
Kata.getMiddle("A") should return "A"
Url: https://www.codewars.com/kata/56747fd5cb988479af000028/
*/
function getMiddle(s) {
const charArray = s.split("");
let num = Math.trunc(charArray.length / 2);
return charArray.length % 2 === 0
? `${charArray[num - 1]}${charArray[num]}`
: charArray[num];
}
/*
Kata 7
Find the divisors!
Create a function named divisors/Divisors that takes an integer n > 1 and returns an array with all of the integer's divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string '(integer) is prime' (null in C#, empty table in COBOL) (use Either String a in Haskell and Result<Vec<u32>, String> in Rust).
Examples:
divisors(12) --> [2, 3, 4, 6]
divisors(25) --> [5]
divisors(13) --> "13 is prime"
Url: https://www.codewars.com/kata/544aed4c4a30184e960010f4/
*/
function divisors(integer) {
arr = [];
for (let i = 1; i <= integer; i++) {
if (integer % i === 0) {
arr.push(i);
}
}
arr.shift();
arr.pop();
return arr.length === 0 ? `${integer} is prime` : arr;
}
/*
Kata 8
Reversed Strings
Complete the solution so that it reverses the string passed into it.
'world' => 'dlrow'
'word' => 'drow'
Url: https://www.codewars.com/kata/5168bb5dfe9a00b126000018/
*/
function solution(str) {
str.split("");
newStr = [];
for (let i = str.length - 1; i >= 0; i--) {
newStr.push(str[i]);
}
return newStr.join("");
}
/*
Kata 9
Count of positives / sum of negatives
Given an array of integers.
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative.
If the input is an empty array or is null, return an empty array.
Example
For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].
Url: https://www.codewars.com/kata/576bb71bbbcf0951d5000044
*/
function countPositivesSumNegatives(input) {
let positiveNum = 0,
negativeSum = 0;
if (!Array.isArray(input) || input.length < 1) {
return [];
} else {
for (let i = 0; i < input.length; i++) {
input[i] > 0 ? positiveNum++ : (negativeSum += input[i]);
}
return [positiveNum, negativeSum];
}
}
/*
Kata 10
Keep Hydrated!
Nathan loves cycling.
Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling.
You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.
For example:
time = 3 ----> litres = 1
time = 6.7---> litres = 3
time = 11.8--> litres = 5
Url: https://www.codewars.com/kata/582cb0224e56e068d800003c
*/
function litres(time) {
return Math.trunc(0.5 * time);
}
/*
Kata 11
Convert boolean values to strings 'Yes' or 'No'.
Complete the method that takes a boolean value and return a "Yes" string for true, or a "No" string for false.
Url: https://www.codewars.com/kata/53369039d7ab3ac506000467
*/
function boolToWord(bool) {
return bool ? "Yes" : "No";
}
/*
Kata 12
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...
Input = {"Ryan", "Kieran", "Jason", "Yous"}
Output = {"Ryan", "Yous"}
Input = {"Peter", "Stephen", "Joe"}
Output = {}
Url: https://www.codewars.com/kata/55b42574ff091733d900002f
*/
function friend(friends) {
friendArray = [];
for (let i = 0; i < friends.length; i++) {
if (friends[i].length === 4) friendArray.push(friends[i]);
}
return friendArray;
}
/*
Kata 13
Remove First and Last Character
It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry about strings with less than two characters.
Url: https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0
*/
function removeChar(str) {
charArray = str.split("");
charArray.pop();
charArray.shift();
return charArray;
}
/*
Kata 14
Ones and Zeros
Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.
Examples:
Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11
However, the arrays can have varying lengths, not just limited to 4.
Url: https://www.codewars.com/kata/578553c3a1b8d5c40300037c
*/
const binaryArrayToNumber = (arr) => {
return parseInt(arr.join(""), 2);
};
/*
Kata 15
Calculate average
Write a function which calculates the average of the numbers in a given list.
Note: Empty arrays should return 0.
Url: https://www.codewars.com/kata/57a2013acf1fa5bfc4000921
*/
function findAverage(array) {
sum = 0;
if (array.length < 1) {
return 0;
} else {
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
}
/*
Kata 16
Square(n) Sum
Complete the square sum function so that it squares each number passed into it and then sums the results together.
For example, for [1, 2, 2] it should return 9 because 1*1 + 2*2 + 2*2 = 9
Url: https://www.codewars.com/kata/515e271a311df0350d00000f
*/
function squareSum(numbers) {
sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum = sum + numbers[i] * numbers[i];
}
return sum;
}
/*
Kata 17
Reverse words
Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.
Examples
"This is an example!" ==> "sihT si na !elpmaxe"
"double spaces" ==> "elbuod secaps"
Url: https://www.codewars.com/kata/5259b20d6021e9e14c0010d4
*/
function reverseWords(str) {
newArray = str.split(" ");
arrayHolder = [];
for (let i = 0; i < newArray.length; i++) {
test2 = newArray[i].split("");
for (let k = test2.length - 1; k >= 0; k--) {
arrayHolder.push(test2[k]);
}
arrayHolder.push(" ");
}
arrayHolder.pop();
return arrayHolder.join("");
}
/*
Kata 18
Sum Arrays
Description:
Write a function that takes an array of numbers and returns the sum of the numbers. The numbers can be negative or non-integer. If the array does not contain any numbers then you should return 0.
Examples
Input: [1, 5.2, 4, 0, -1]
Output: 9.2
Input: []
Output: 0
Input: [-2.398]
Output: -2.398
Assumptions
You can assume that you are only given numbers.
You cannot assume the size of the array.
You can assume that you do get an array and if the array is empty, return 0"
Url: https://www.codewars.com/kata/53dc54212259ed3d4f00071c
*/
// Sum Numbers
function sum(numbers) {
count = 0;
if (!numbers) {
return 0;
} else {
for (let i = 0; i < numbers.length; i++) {
count += numbers[i];
}
return count;
}
}
/*
Kata 19
Function 1 - hello world
Make a simple function called greet that returns the most-famous "hello world!".
Style Points
Sure, this is about as easy as it gets. But how clever can you be to create the most creative "hello world" you can think of? What is a "hello world" solution you would want to show your friends?
Url: https://www.codewars.com/kata/523b4ff7adca849afe000035
*/
const greet = () => {
return "hello world!";
};
/*
Kata 20
List Filtering
Description:
In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out.
Example
filter_list([1,2,'a','b']) == [1,2]
filter_list([1,'a','b',0,15]) == [1,0,15]
filter_list([1,2,'aasf','1','123',123]) == [1,2,123]
Url: https://www.codewars.com/kata/53dbd5315a3c69eed20002dd
*/
function filter_list(l) {
newChar = [];
for (let i = 0; i < l.length; i++) {
if (typeof l[i] !== "string") {
newChar.push(l[i]);
}
}
return newChar;
}
/*
Kata 21
Counting sheep...
Description:
Consider an array/list of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).
For example,
[true, true, true, false,
true, true, true, true ,
true, false, true, false,
true, false, false, true ,
true, true, true, true ,
false, false, true, true]
The correct answer would be 17.
Hint: Don't forget to check for bad values like null/undefined
Url: https://www.codewars.com/kata/54edbc7200b811e956000556
*/
function countSheeps(sheep) {
count = 0;
if (sheep.length <= 0) {
return count;
} else {
for (let i = 0; i < sheep.length; i++) {
if (sheep[i] === true) count++;
}
return count;
}
}
/*
Kata 22
How good are you really?
There was a test in your class and you passed it. Congratulations!
But you're an ambitious person. You want to know if you're better than the average student in your class.
You receive an array with your peers' test scores. Now calculate the average and compare your score!
Return true if you're better, else false!
Note:
Your points are not included in the array of your class's points. Do not forget them when calculating the average score!
Url: https://www.codewars.com/kata/5601409514fc93442500010b/
*/
function betterThanAverage(classPoints, yourPoints) {
average = 0;
for (const points of classPoints) {
average += points;
}
average /= classPoints.length;
console.log(average);
return yourPoints > average ? true : false;
}
/*
Kata 21
Are You Playing Banjo?
Description:
Create a function which answers the question "Are you playing banjo?".
If your name starts with the letter "R" or lower case "r", you are playing banjo!
The function takes a name as its only argument, and returns one of the following strings:
name + " plays banjo"
name + " does not play banjo"
Names given are always valid strings.
Url: https://www.codewars.com/kata/53af2b8861023f1d88000832/
*/
function areYouPlayingBanjo(name) {
charArray = name.split("");
return charArray[0] === "R" || charArray[0] === "r"
? `${name} plays banjo`
: `${name} does not plays banjo`;
}
/*
Kata 22
Binary Addition
Description:
Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.
The binary number returned should be a string.
Examples:(Input1, Input2 --> Output (explanation)))
1, 1 --> "10" (1 + 1 = 2 in decimal or 10 in binary)
5, 9 --> "1110" (5 + 9 = 14 in decimal or 1110 in binary)
Url: https://www.codewars.com/kata/551f37452ff852b7bd000139
*/
function addBinary(a, b) {
return (a + b).toString(2);
}
/*
Kata 23
Categorize New Member
Description:
The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed.
To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.
Input
Input will consist of a list of pairs. Each pair contains information for a single potential member. Information consists of an integer for the person's age and an integer for the person's handicap.
Output
Output will consist of a list of string values (in Haskell and C: Open or Senior) stating whether the respective member is to be placed in the senior or open category.
Example
input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]
output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]
Url: https://www.codewars.com/kata/5502c9e7b3216ec63c0001aa
*/
function openOrSenior(data) {
const output = [];
for (const [i, n] of data) {
console.log(i, n);
if (i >= 55 && n > 7) output.push("Senior");
else output.push("Open");
}
return output;
}
/*
Kata 24
Opposites Attract
Description:
Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.
Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren't.
Url: https://www.codewars.com/kata/555086d53eac039a2a000083
*/
function lovefunc(flower1, flower2) {
return flower1 % 2 !== flower2 % 2;
}
/*
Kata 25
DNA to RNA Conversion
Description:
Deoxyribonucleic acid, DNA is the primary information storage molecule in biological systems. It is composed of four nucleic acid bases Guanine ('G'), Cytosine ('C'), Adenine ('A'), and Thymine ('T').
Ribonucleic acid, RNA, is the primary messenger molecule in cells. RNA differs slightly from DNA its chemical structure and contains no Thymine. In RNA Thymine is replaced by another nucleic acid Uracil ('U').
Create a function which translates a given DNA string into RNA.
For example:
"GCAT" => "GCAU"
The input string can be of arbitrary length - in particular, it may be empty. All input is guaranteed to be valid, i.e. each input string will only ever consist of 'G', 'C', 'A' and/or 'T'.
Url: https://www.codewars.com/kata/5556282156230d0e5e000089/
*/
function DNAtoRNA(dna) {
const charArray = dna.split("");
for (let i = 0; i < charArray.length; i++)
if (charArray[i] === "T") charArray[i] = "U";
return charArray.join("");
}
/*
Kata 26
Odd or Even?
Description:
Given a list of integers, determine whether the sum of its elements is odd or even.
Give your answer as a string matching "odd" or "even".
If the input array is empty consider it as: [0] (array with a zero).
Examples:
Input: [0]
Output: "even"
Input: [0, 1, 4]
Output: "odd"
Input: [0, -1, -5]
Output: "even"
Url: https://www.codewars.com/kata/5949481f86420f59480000e7
*/
function oddOrEven(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) sum += array[i];
return sum % 2 === 0 ? "even" : "odd";
}
/*
Kata 27
Find the smallest integer in the array
Description:
Given an array of integers your solution should find the smallest integer.
For example:
Given [34, 15, 88, 2] your solution will return 2
Given [34, -345, -1, 100] your solution will return -345
You can assume, for the purpose of this kata, that the supplied array will not be empty.
Url: https://www.codewars.com/kata/55a2d7ebe362935a210000b2/
*/
function findSmallestInt(arr) {
let smallest = arr[0];
for (const number of arr) {
if (smallest > number) smallest = number;
}
return smallest;
}
/*
Kata 28
Beginner - Reduce but Grow
Description:
Given a non-empty array of integers, return the result of multiplying the values together in order. Example:
[1, 2, 3, 4] => 1 * 2 * 3 * 4 = 24
Url: https://www.codewars.com/kata/57f780909f7e8e3183000078
*/
function grow(x) {
let value = 1;
for (const number of x) {
value = value * number;
}
return value;
}
/*
Kata 29
Disemvowel Trolls
Description:
Trolls are attacking your comment section!
A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.
Your task is to write a function that takes a string and return a new string with all vowels removed.
For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".
Note: for this kata y isn't considered a vowel.
Url: https://www.codewars.com/kata/52fba66badcd10859f00097e
*/
function disemvowel(str) {
const firstArr = [];
const vowels = ["a", "A", "e", "E", "i", "I", "o", "O", "u", "U"];
const test = str.split(" ");
for (const l of test) {
firstArr.push(
l
.split("")
.filter((cur) => (!vowels.includes(cur) ? cur : ""))
.join("")
);
}
return firstArr.join(" ");
}
/*
Kata 30
Descending Order
Description:
Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.
Examples:
Input: 42145 Output: 54421
Input: 145263 Output: 654321
Input: 123456789 Output: 987654321
Url: https://www.codewars.com/kata/5467e4d82edf8bbf40000155/
*/
function descendingOrder(n) {
return Number(
n
.toString()
.split("")
.sort((a, b) => b - a)
.join("")
);
}
/*
Kata 31
Convert a string to an array
Description:
Write a function to split a string and convert it into an array of words.
Examples (Input ==> Output):
"Robin Singh" ==> ["Robin", "Singh"]
"I love arrays they are my favorite" ==> ["I", "love", "arrays", "they", "are", "my", "favorite"]
Url: https://www.codewars.com/kata/57e76bc428d6fbc2d500036d
*/
function stringToArray(string) {
return string.split(" ");
}
/*
Kata 32
String ends with?
Description:
Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
Examples:
solution('abc', 'bc') // returns true
solution('abc', 'd') // returns false
Url: https://www.codewars.com/kata/51f2d1cafc9c0f745c00037d
*/
function solution(str, ending) {
arrayStr = str.split("");
endingStr = ending.split("");
test = arrayStr.slice(arrayStr.length - endingStr.length).join("");
return test === ending ? true : false;
}
/*
Kata 33
Convert number to reversed array of digits
Description:
Given a random non-negative number, you have to return the digits of this number within an array in reverse order.
Example(Input => Output):
35231 => [1,3,2,5,3]
0 => [0]
Url: https://www.codewars.com/kata/5583090cbe83f4fd8c000051
*/
function digitize(n) {
return n.toString().split("").map(Number).reverse();
}
/*
Kata 34
Remove String Spaces
Description:
Write a function that removes the spaces from the string, then return the resultant string.
Examples (Input -> Output):
"8 j 8 mBliB8g imjB8B8 jl B" -> "8j8mBliB8gimjB8B8jlB"
"8 8 Bi fk8h B 8 BB8B B B B888 c hl8 BhB fd" -> "88Bifk8hB8BB8BBBB888chl8BhBfd"
"8aaaaa dddd r " -> "8aaaaaddddr"
Url: https://www.codewars.com/kata/57eae20f5500ad98e50002c5/
*/
function noSpace(x) {
return x.replaceAll(" ", "");
}
/*
Kata 35
Highest and Lowest
Description:
In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.
Examples
highAndLow("1 2 3 4 5"); // return "5 1"
highAndLow("1 2 -3 4 5"); // return "5 -3"
highAndLow("1 9 3 4 -5"); // return "9 -5"
Notes
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
Url: https://www.codewars.com/kata/554b4ac871d6813a03000035/
*/
function highAndLow(numbers) {
const test = numbers
.split(" ")
.map(Number)
.sort((a, b) => b - a);
return `${test[0]} ${test[test.length - 1]}`;
}
highAndLow("1 2 -3 4 5");
/*
Kata 36
The highest profit wins!
Description:
Story
Ben has a very simple idea to make some profit: he buys something and sells it again. Of course, this wouldn't give him any profit at all if he was simply to buy and sell it at the same price. Instead, he's going to buy it for the lowest possible price and sell it at the highest.
Task
Write a function that returns both the minimum and maximum number of the given list/array.
Examples (Input --> Output)
[1,2,3,4,5] --> [1,5]
[2334454,5] --> [5,2334454]
[1] --> [1,1]
Remarks
All arrays or lists will always have at least one element, so you don't need to check the length. Also, your function will always get an array or a list, you don't have to check for null, undefined or similar.
Url: https://www.codewars.com/kata/554b4ac871d6813a03000035/
*/
function minMax(arr) {
return [Math.min(...arr), Math.max(...arr)];
}
/*
Kata 37
Area or Perimeter
Description:
You are given the length and width of a 4-sided polygon. The polygon can either be a rectangle or a square.
If it is a square, return its area. If it is a rectangle, return its perimeter.
Example(Input1, Input2 --> Output):
6, 10 --> 32
3, 3 --> 9
Note: for the purposes of this kata you will assume that it is a square if its length and width are equal, otherwise it is a rectangle.
Url: https://www.codewars.com/kata/5ab6538b379d20ad880000ab/
*/
const areaOrPerimeter = function (l, w) {
return l === w ? l * w : l * 2 + w * 2;
};
/*
Kata 38
Two Oldest Ages
Description:
The two oldest ages function/method needs to be completed. It should take an array of numbers as its argument and return the two highest numbers within the array. The returned value should be an array in the format [second oldest age, oldest age].
The order of the numbers passed in could be any order. The array will always include at least 2 items. If there are two or more oldest age, then return both of them in array format.
For example (Input --> Output):
[1, 2, 10, 8] --> [8, 10]
[1, 5, 87, 45, 8, 8] --> [45, 87]
[1, 3, 10, 0]) --> [3, 10]
Url: https://www.codewars.com/kata/511f11d355fe575d2c000001/
*/
function twoOldestAges(ages) {
const num = ages.sort((a, b) => b - a);
return [num[1], num[0]];
}
/*
Kata 39
Bubblesort Once
Description:
Bubblesort is an inefficient sorting algorithm that is simple to understand and therefore often taught in introductory computer science courses as an example how not to sort a list. Nevertheless, it is correct in the sense that it eventually produces a sorted version of the original list when executed to completion.
At the heart of Bubblesort is what is known as a pass. Let's look at an example at how a pass works.
Consider the following list:
9, 7, 5, 3, 1, 2, 4, 6, 8
We initiate a pass by comparing the first two elements of the list. Is the first element greater than the second? If so, we swap the two elements. Since 9 is greater than 7 in this case, we swap them to give 7, 9. The list then becomes:
7, 9, 5, 3, 1, 2, 4, 6, 8
We then continue the process for the 2nd and 3rd elements, 3rd and 4th elements ... all the way up to the last two elements. When the pass is complete, our list becomes:
7, 5, 3, 1, 2, 4, 6, 8, 9
Notice that the largest value 9 "bubbled up" to the end of the list. This is precisely how Bubblesort got its name.
Task
Given an array of integers, your function bubblesortOnce/bubblesort_once/BubblesortOnce (or equivalent, depending on your language's naming conventions) should return a new array equivalent to performing exactly 1 complete pass on the original array. Your function should be pure, i.e. it should not mutate the input array.
Url: https://www.codewars.com/kata/56b97b776ffcea598a0006f2/
*/
function bubblesortOnce(a) {
let result = [...a];
let pass;
for (let i = 0; i < result.length - 1; i++) {