-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
1840 lines (1416 loc) · 46.9 KB
/
app.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
//NUMBER
/*
*/
//MY SOLUTION
/*********THE ABOVE IS A TEMPLATE*************** */
//NUMBER 68 failed kata
/*
ou have clothes international size as text (xs, s, xxl).
Your goal is to return European number size as a number from that size.
Notice that there is arbitrary amount of modifiers (x), excluding m size, and input can be any string.
Linearity
Base size for medium (m) is 38.
(then, small (s) is 36, and large (l) is 40)
The scale is linear; modifier x continues that by adding or subtracting 2 from resulting size.
(For sizes where x modifier makes total size negative, treat that as OK, and return negative size)
Invalid cases
Function should handle wrong/invalid sizes.
Valid input has any base size (s/m/l) and any amount of modifiers (x) before base size (like xxl).
Notice that you cannot apply modifier for m size, consider that case as invalid!
Anything else is disallowed and should be considered as invalid (None for Python, 0, false for Go, null for JavaScript).
Invalid examples: xxx, sss, xm, other string
*/
//MY SOLUTION by deepseek AI
function sizeToNumber(size) {
// Handle invalid input (empty string, null, undefined, etc.)
if (!size || typeof size !== 'string') {
return null;
}
// Convert to lowercase for case insensitivity
size = size.toLowerCase();
// Find the base size (s, m, l) and count the number of 'x' modifiers
let baseSize = '';
let xCount = 0;
for (let i = 0; i < size.length; i++) {
const char = size[i];
if (char === 's' || char === 'm' || char === 'l') {
// If base size is already found, it's invalid (e.g., 'ss', 'sm')
if (baseSize !== '') {
return null;
}
baseSize = char;
} else if (char === 'x') {
xCount++;
} else {
// If any invalid character is found, return null
return null;
}
}
// If no base size is found, it's invalid
if (baseSize === '') {
return null;
}
// Check if modifiers are applied to 'm' (invalid case)
if (baseSize === 'm' && xCount > 0) {
return null;
}
// Calculate the base European size
let europeanSize;
switch (baseSize) {
case 's':
europeanSize = 36;
break;
case 'm':
europeanSize = 38;
break;
case 'l':
europeanSize = 40;
break;
default:
return null; // Invalid base size (should not happen)
}
// Apply modifiers
// For 's' and 'l', each 'x' subtracts or adds 2, respectively
if (baseSize === 's') {
europeanSize -= xCount * 2; // Subtract for smaller sizes
} else if (baseSize === 'l') {
europeanSize += xCount * 2; // Add for larger sizes
}
// Return the final European size
return europeanSize;
}
//NUMBER 67 -- failed kata
/*
given an array of integers, which represent the stones, and your task is
to assemble the highest-value pyramid from them. The pyramid is made out of exactly three layers, containing:
Top layer: 1 integer
Middle layer: 2 identical integers
Bottom layer: 3 identical integers
Additionally, no integer can appear in two or more layers. That is,
each layer is made from stones of the same value, and stones of the same value can be used at most in one layer.
Graphically, the structure of the pyramid looks like this, where
x
≠
y
≠
z
x
=y
=z
[z]
[y] [y]
[x][x][x]
Input
The input is an array that may contain positive and negative integers,
as well as zeros. The integers are in no specific order and can be repeated. The array may also be empty.
Output
The output is a single integer – the sum of all integers that make up the pyramid.
For example, given input [1,1,1,2,2,2,3,3,3], the highest-value pyramid is:
[1]
[2] [2]
[3][3][3]
And the sum is thus:
3
⋅
3
+
2
⋅
2
+
1
=
14
3⋅3+2⋅2+1=14.
If it's not possible to build a
pyramid from the given array (e.g. [-1,-1,0,0,1,1] or an empty one []), return None, null,
or other language-specific alternative.
*/
//MY SOLUTION
function pyramid(stones) {
let prepStones = stones.sort((a, b) => a - b)
let layerOne = prepStones[0]
let layerTwo = []
let layerThree = []
let sumTwo
let sumThree
for (let i = 0; i < prepStones.length; i++) {
if ((layerOne !== prepStones[i]) && layerTwo.length !== 2) {
layerTwo.push(prepStones[i])
sumTwo = layerTwo[0] + layerTwo[1]
}
if ((layerOne !== prepStones[i]) && layerTwo.includes(prepStones[i]) == false && layerThree.length !== 3) {
layerThree.push(prepStones[i])
sumThree = layerThree[0] + layerThree[1] + layerThree[2]
}
}
let finals = layerOne + sumTwo + sumThree
}
//NUMBER 66 failed kata
/*
Multiply the adjacent digits which are not separated by a '-' or a '+' in a string, then do the sum.
For example:
"53+5" -> 20, which is 5 * 3 + 5
"266-66" -> 36, which is 2 * 6 * 6 - 6 * 6
"555" -> 125, which is 5 * 5 * 5
*/
//MY SOLUTION
function digitMultiplication(expr) {
let multiBoxLeft = []
let multiBoxRight = []
if (expr.includes('+')) {
multiBoxLeft.push(expr[0], expr[expr.indexOf('+') - 1])
multiBoxRight.push(expr[expr.indexOf('+') + 1], expr[expr.length - 1])
}
return multiBoxRight
}
//NUMBER 65
/*
Your task is to create a function that takes two parameters:
text: A string containing the text to be wrapped in comments.
style: A string indicating the style of comments to use. It can be one of the following:
"Bash"
"Bash Multiline"
"JavaDoc"
"Python"
"Python Multiline"
"Javascript"
"Javascript Multiline"
"SGML"
"SQL"
The function should wrap the given text in the appropriate comment style and return the result.
*/
//MY SOLUTION
function comment(text, style) {
const lines=text.split('\n')
switch(style) {
case "Bash":
case "Python":
return lines.map(line=>`# ${line}`).join('\n')
break;
case "Bash Multiline":
return `: "\n${text}\n"`
break;
case "JavaDoc":
return `/**\n${lines.map(line=> `* ${line}`).join('\n')}\n*/`
break;
case "Python Multiline":
return `"""\n${text}\n"""`
break;
case "Javascript":
return lines.map(line=>`// ${line}`).join('\n')
break;
case "Javascript Multiline":
return `/*\n${text}\n*/`
break;
case "SGML":
return lines.map(line=>`<!-- ${line} -->`).join('\n')
break;
case "SQL":
return lines.map(line=>`-- ${line}`).join('\n')
default:
return "please enter your language"
}
}
//NUMBER 64
/*
Shouldn't the two functions getMax1 and getMax2 be equivalent and return the same value?
Can you spot a problem and fix it? Can you learn something about JavaScript style in this kata?
*/
//MY SOLUTION
function getMax1()
{
var max =
{
name: 'Max Headroom'
}
return max;
}
function getMax2()
{
return getMax1()
}
//NUMBER 63
/*
Complete the Person object, by completing the FillFriends function to fill the _friends Array for the person object.
*/
//MY SOLUTION
var Person = function(){
var person = {
_name: "Leroy",
_friends: [],
fillFriends(f) {
this._friends = this._friends.concat(f)
}
}
return person;
}
//NUMBER 62
/*
In JavaScript, there is a special case where strict comparison of the same variable returns false!
Try to find out what must be done to get such result!
var x = something;
x === x // returns false!
Write a function which will return value for which strict comparison will give false!
*/
//MY SOLUTION
function findStrangeValue() {
return NaN
}
//NUMBER 61
/*
You are running the calculation 2 + 2 * 2 + 2 * 2 and expect to get the answer 32 but instead the function
keeps returning 10. Fix the function to make it return 32 without changing the number or the operators.
*/
//MY SOLUTION
function orderOperations () {
return (2 + 2) * (2 + 2) * 2
}
//NUMBER 60
/*
While making a zork-type game, you create an object of rooms. Unfortunately,
the game is not working. Find all of the errors in the rooms object to get your game working again.
*/
//MY SOLUTION
var rooms = {
first: {
description: 'This is the first room',
items: {
chair: 'The old chair looks comfortable',
lamp: 'This lamp looks ancient'
}
},
second: {
description: 'This is the second room',
items: {
couch: 'This couch looks like it would hurt your back',
table: 'On the table there is an unopened bottle of water'
}
}
}
//NUMBER 59
/*
In case you got lost, here's precisely what you have to do: define a method Number.prototype.times that accepts a function f as an
argument and executes it as many times as the integer it is called on (e.g. (100).times would execute something 100 times).
The iteration variable i should be supplied to the anonymous function being executed in order to support looping through array elements.
*/
//MY SOLUTION
Number.prototype.times = function (f) {
for (let i = 0; i < this; i++) {
f(i);
}
}
//NUMBER 58
/*
You are given a program squaresOnly that accepts an array of natural numbers up to and including 100 (and including 0)
of length >= 1, array, and returns a new array containing only square numbers that have appeared in the input array.
Refactor the solution to use as few characters as possible. There is a maximum character limit of 127. Here are a few hints:
There are a lot of handy built-in Array methods in Javascript that you may have never heard of even after completing a basic
course in Javascript (e.g. those provided by Codecademy) - well, at least I haven't heard of until quite recently. You may also
want to research any new built-in methods offered by ES6, the newest specification of Javascript at the time of writing.
Don't you think the array parameter is a bit wordy? ;)
Good luck! :D
*/
//MY SOLUTION
function squaresOnly(array) {
return array.filter(x => Number.isInteger(Math.sqrt(x)))
}
//NUMBER 57
/*
You are given a program sumSquares that takes an array as input and returns the sum of the squares of each item in an array. For example:
sumSquares([1,2,3,4,5]) === 55 // 1 ** 2 + 2 ** 2 + 3 ** 2 + 4 ** 2 + 5 ** 2
sumSquares([7,3,9,6,5]) === 200
sumSquares([11,13,15,18,2]) === 843
*/
//MY SOLUTION
function sumSquares(array) {
const squaredNumbers = array.map(number => number * number)
return squaredNumbers.reduce((a,b)=> a + b)
}
//NUMBER 56
/*
Not all integers can be represented by JavaScript/TypeScript. It has space to to represent 53bit signed integers.
In this Kata, we've to determine if it is safe to use the integer or not. Make use of the latest ES6 features to find this.
SafeInteger(9007199254740990) //true
SafeInteger(-90) //true
SafeInteger(9007199254740992) //false
*/
//MY SOLUTION
function SafeInteger(n) {
return Number.isSafeInteger(n)
}
//NUMBER 55
/*
You task to pass only this tests :
a == false
!a == false
a == !a
*/
//MY SOLUTION
const a = new Boolean(false)
//NUMBER 54
/*
The function takes in 2 inputs x and y, and should return x to the power of y
Simple, right? But you're NOT allowed to use Math.pow();
Obs: x and y will be naturals, so DON'T take fractions into consideration;
Note : zero to the power of zero equals one in this kata
Great coding <3
*/
//MY SOLUTION
function power(x,y){
let result = 1;
for (let i = 0; i < y; i++) {
result *= x;
}
return result
}
//NUMBER 53
/*
When it's spring Japanese cherries blossom, it's called "sakura" and it's admired a lot. The petals start to fall in late April.
Suppose that the falling speed of a petal is 5 centimeters per second (5 cm/s), and it takes 80 seconds for the petal to reach the
ground from a certain branch.
Write a function that receives the speed (in cm/s) of a petal as input, and returns the time it takes for that petal to reach the
ground from the same branch.
Notes:
The movement of the petal is quite complicated, so in this case we can see the velocity as a constant during its falling.
Pay attention to the data types.
If the initial velocity is non-positive, the return value should be 0
*/
//MY SOLUTION
function sakuraFall(v) {
if (v<0) {
return 0
}
else if (v === 0) {
return 0;
}
else {
const height = 400;
return height / v;
}
}
//NUMBER 52
/*
Your task is to simply calculate the loudness of a sound wave, given its intensity as a parameter to the dBScale/db_scale function.
*/
//MY SOLUTION
function dBScale(intensity) {
const referenceIntensity = 1e-12;
return 10 * Math.log10(intensity / referenceIntensity);
}
//NUMBER 51
/*
Create a package.json configuration and assign it to the configuration constant so it can be tested.
To pass the kata you need only the least that a real package file would require.
*/
//MY SOLUTION
const configuration = {
"name": "foo",
"version": "1.2.3",
"description": "A packaged foo fooer for fooing foos"
};
//NUMBER 50
/*
Prolog:
This kata series was created for friends of mine who just started to learn programming. Wish you all the best and keep your mind open and sharp!
Task:
Write a function that will accept two parameters: variable and type and check if type of variable is matching type.
Return true if types match or false if not.
Examples:
42, "number" --> true
"42", "number" --> false
*/
//MY SOLUTION
function typeValidation(variable, type) {
return (typeof variable === type) ? true :false;
}
//NUMBER 49
/*
The goal is to create a function of two inputs number and power, that "raises" the number up to power (ie multiplies number by itself power times).
Examples
numberToPower(3, 2) // -> 9 ( = 3 * 3 )
numberToPower(2, 3) // -> 8 ( = 2 * 2 * 2 )
numberToPower(10, 6) // -> 1000000
Note: Math.pow and some other Math functions like eval() and ** are disabled.
*/
//MY SOLUTION
function numberToPower(number, power){
console.info(Math.log2(1024));
let result = 1;
for (let i = 0; i < power; i++) {
result *= number;
}
return result
}
//NUMBER 48
/*
You are given a function describeAge / describe_age that takes a parameter age (which will always be a positive integer) and does the following:
If the age is 12 or lower, it return "You're a(n) kid"
If the age is anything between 13 and 17 (inclusive), it return "You're a(n) teenager"
If the age is anything between 18 and 64 (inclusive), it return "You're a(n) adult"
If the age is 65 or above, it return "You're a(n) elderly"
Your task is to shorten the code as much as possible. Note that submitting the given code will not work because there is a character limit of 137.
*/
//MY SOLUTION
function describeAge(age) {
return `You're a(n) ${age < 13 ? 'kid' : age < 18 ? 'teenager' : age < 65 ? 'adult' : 'elderly'}`;
}
//NUMBER 47
/*
Function should return a dictionary/Object/Hash with "status" as a key, whose value can : "busy" or "available"
depending on the truth value of the argument is_busy.
*/
//MY SOLUTION
function getStatus(isBusy) {
var msg = (isBusy ? "busy" : "available");
return {
status: msg
}
}
//NUMBER 46
/*
Write a function howMuchWater (JS)/how_much_water (Python and Ruby) to work out how much water is needed
if you have a clothes amount of clothes. The function will accept 3 arguments: - water, load (or max_loadin Ruby) and clothes.
*/
//MY SOLUTION
function howMuchWater(water, load, clothes){
let tooMany = load * 2
if (clothes < load) {
return 'Not enough clothes'
}
else if (clothes > tooMany) {
return 'Too much clothes'
}
else {
const waterNeeded = water * Math.pow(1.1, clothes - load);
return Number(waterNeeded.toFixed(2));
}
}
//NUMBER 45
/*
For this problem you must create a program that says who ate the last cookie. If the input is a string then "Zach" ate the cookie.
If the input is a float or an int then "Monica" ate the cookie. If the input is anything else "the dog" ate the cookie. The way to
return the statement is: "Who ate the last cookie? It was (name)!"
Ex: Input = "hi" --> Output = "Who ate the last cookie? It was Zach! (The reason you return Zach is because the input is a string)
Note: Make sure you return the correct message with correct spaces and punctuation.
Please leave feedback for this kata. Cheers!
*/
//MY SOLUTION
function cookie(x){
if (typeof x === "string") {
return "Who ate the last cookie? It was Zach!"
}
else if (x === Number(x)) {
return "Who ate the last cookie? It was Monica!"
}
else {
return "Who ate the last cookie? It was the dog!"
}
}
//NUMBER 44
/*
This method, which is supposed to return the result of dividing its first argument by its second, isn't always returning correct values. Fix it.
*/
//MY SOLUTION
function solve(x, y) {
return x/y
}
//NUMBER 43
/*
Write a function that accepts arbitrary X and Y resolutions and converts them into resolutions with a 16:9 aspect
ratio that maintain equal height. Round your answers up to the nearest integer.
*/
//MY SOLUTION
function aspectRatio(x,y){
let ratio = []
ratio.push(Math.ceil((y*16)/9))
ratio.push(y)
return ratio
}
//NUMBER 42
/*
Create a function that finds the integral of the expression passed.
In order to find the integral all you need to do is add one to the exponent (the second argument),
and divide the coefficient (the first argument) by that new number.
For example for 3x^2, the integral would be 1x^3: we added 1 to the exponent, and divided the coefficient by that new number).
Notes:
The output should be a string.
The coefficient and exponent is always a positive integer.
Examples
3, 2 --> "1x^3"
12, 5 --> "2x^6"
20, 1 --> "10x^2"
40, 3 --> "10x^4"
90, 2 --> "30x^3"
*/
//MY SOLUTION
function integrate(coefficient, exponent) {
let theExponent = exponent + 1
let theCoefficient = coefficient/theExponent
let theIntegral = `${theCoefficient.toString()}x^${theExponent}`
return theIntegral
}
//NUMBER 41
/*
We have implemented a function wrap(value) that takes a value of arbitrary type and wraps it in a new JavaScript
Object or Python Dict setting the 'value' key on the new Object or Dict to the passed-in value.
So, for example, if we execute the following code:
wrapper_obj = wrap("my_wrapped_string");
# wrapper_obj should be {"value":"my_wrapped_string"}
We would then expect the following statement to be true:
wrapper_obj["value"] == "my_wrapped_string"
Unfortunately, the code is not working as designed. Please fix the code so that it behaves as specified.
*/
//MY SOLUTION
function wrap(value) {
return {
value:value
};
}
//NUMBER 40
/*
In this kata, the function will take a string as its argument, and return a string with every word replaced by the explanation to everything,
according to Freud. Note that an empty string, or no arguments, should return an empty string.
*/
//MY SOLUTION
function toFreud(string) {
let newString = string.split(' ')
if (string==="") {
return ""
}
else {
for (let i = 0; i < newString.length; i++) {
newString[i] = 'sex'
}
}
return newString.join(' ')
}
//NUMBER 39
/*
Given a string, write a function that returns the string with a question mark ("?") appends to the end,
unless the original string ends with a question mark, in which case, returns the original string.
For example (Input --> Output)
"Yes" --> "Yes?"
"No?" --> "No?"
*/
//MY SOLUTION
function ensureQuestion(s) {
return (s.includes('?')) ? s : s + '?'
}
//NUMBER 38
/*
Your task is to create userlinks for the url, you will be given a username and must return a valid link.
Example
generate_link('matt c')
http://www.codewars.com/users/matt%20c
*/
//MY SOLUTION
function generateLink(user) {
let encodedUser = encodeURIComponent(user)
return 'http://www.codewars.com/users/' + encodedUser
}
//NUMBER 37
/*
You've decided to write a function, guessBlue() to help automatically calculate whether you should guess "blue" or "red".
The function should take four arguments:
the number of blue marbles you put in the bag to start
the number of red marbles you put in the bag to start
the number of blue marbles pulled out so far (always lower than the starting number of blue marbles)
the number of red marbles pulled out so far (always lower than the starting number of red marbles)
guessBlue() should return the probability of drawing a blue marble, expressed as a float. For example, guessBlue(5, 5, 2, 3) should return 0.6.
*/
//MY SOLUTION
function guessBlue(blueStart, redStart, bluePulled, redPulled) {
return (blueStart - bluePulled) / ((blueStart - bluePulled) + (redStart - redPulled))
}
//NUMBER 36
/*
This is a beginner friendly kata especially for UFC/MMA fans.
It's a fight between the two legends: Conor McGregor vs George Saint Pierre in Madison Square Garden.
Only one fighter will remain standing, and after the fight in an interview with Joe Rogan the winner will make his legendary statement.
It's your job to return the right statement depending on the winner!
If the winner is George Saint Pierre he will obviously say:
"I am not impressed by your performance."
If the winner is Conor McGregor he will most undoubtedly say:
"I'd like to take this chance to apologize.. To absolutely NOBODY!"
Good Luck!
*/
//MY SOLUTION
var quote = function(fighter) {
let winner = fighter.toLowerCase()
return (winner === "george saint pierre") ? "I am not impressed by your performance." : "I'd like to take this chance to apologize.. To absolutely NOBODY!"
};
//NUMBER 35
/*
You received a whatsup message from an unknown number. Could it be from that girl/boy with a foreign accent you met yesterday evening?
Write a simple function to check if the string contains the word hallo in different languages.
These are the languages of the possible people you met the night before:
hello - english
ciao - italian
salut - french
hallo - german
hola - spanish
ahoj - czech republic
czesc - polish
Notes
you can assume the input is a string.
to keep this a beginner exercise you don't need to check if the greeting is a subset of word (Hallowen can pass the test)
function should be case insensitive to pass the tests
*/
//MY SOLUTION
function validateHello(greetings) {
let translateGreeting = greetings.toLowerCase()
return (translateGreeting.includes('hello') || translateGreeting.includes('ciao') || translateGreeting.includes('salut') || translateGreeting.includes('hallo') ||
translateGreeting.includes('hola') || translateGreeting.includes('ahoj') || translateGreeting.includes('czesc')) ? true : false
}
//NUMBER 34
/*
Give you two strings: s1 and s2. If they are opposite, return true; otherwise, return false.
Note: The result should be a boolean value, instead of a string.
The opposite means: All letters of the two strings are the same, but the case is opposite.
you can assume that the string only contains letters or it's a empty string.
Also take note of the edge case - if both strings are empty then you should return false/False.
Examples (input -> output)
"ab","AB" -> true
"aB","Ab" -> true
"aBcd","AbCD" -> true
"AB","Ab" -> false
"","" -> false
*/
//MY SOLUTION
function isOpposite(s1,s2){
if (s1 === '' || s2 === '' || s1.length !== s2.length) {
return false
}
else {
for (let i = 0; i < s1.length; i++) {
if((s1[i].toLowerCase() !== s2[i].toLowerCase()) || (s1[i] === s2[i])) {
return false
}
}
return true
}
}
//NUMBER 33
/*
Let's imagine we have a popular online RPG. A player begins with a score of 0 in class E5. A1 is the highest level a player can achieve.
Now let's say the players wants to rank up to class E4. To do so the player needs to achieve at least 100 points to enter the qualifying stage.
Write a script that will check to see if the player has achieved at least 100 points in his class. If so, he enters the qualifying stage.
In that case, we return, "Well done! You have advanced to the qualifying stage. Win 2 out of your next 3 games to rank up.".
Otherwise return, False/false (according to the language in use).
NOTE
: Remember, in C# you have to cast your output value to Object type!
*/
//MY SOLUTION
function playerRankUp (points) {
if (points >= 100) {
return "Well done! You have advanced to the qualifying stage. Win 2 out of your next 3 games to rank up."
}
else {
return false
}
}
//NUMBER 32
/*
Given an array of 4 integers
[a,b,c,d] representing two points (a, b) and (c, d), return a string representation of the slope of the line joining these two points.
For an undefined slope (division by 0), return undefined . Note that the "undefined" is case-sensitive.
a:x1
b:y1
c:x2
d:y2
Assume that [a,b,c,d] and the answer are all integers (no floating numbers!).
*/
//MY SOLUTION
function slope(points)
{
let topPortion = points[3] - points[1]
let bottomPortion = points[2] - points[0]
if (bottomPortion === 0) {
return "undefined"
}
else {
return (topPortion / bottomPortion).toString()
}
}
//NUMBER 31
/*
In this kata, your job is to return the two distinct highest values in a list.
If there're less than 2 unique values, return as many of them, as possible.
The result should also be ordered from highest to lowest.
Examples:
[4, 10, 10, 9] => [10, 9]
[1, 1, 1] => [1]
[] => []
*/
//MY SOLUTION
function twoHighest(arr) {
let arrCollection = []
let subCollection = arr.sort((a,b)=>b-a)
for (let i = 0; i < subCollection.length && arrCollection.length < 2; i++) {
if (!arrCollection.includes(subCollection[i])) {
arrCollection.push(subCollection[i])
}
}
return arrCollection
}
//NUMBER 30
/*
You are creating a text-based terminal version of your favorite board game. In the board game,
each turn has six steps that must happen in this order: roll the dice, move, combat, get coins, buy more health, and print status.
You are using a library that already has the functions below. Create a function named main that calls the functions in the proper order.
- combat
- buyHealth
- getCoins
- printStatus
- rollDice
- move
*/
//MY SOLUTION
var health = 100
var position = 0