@@ -21,3 +21,56 @@ function simpleArraySum(ar: number[]): number {
21
21
// Return the sum of the array
22
22
return sum ;
23
23
}
24
+
25
+ // 003: compare the triplets
26
+
27
+ /*
28
+ * Complete the 'compareTriplets' function below.
29
+ *
30
+ * The function is expected to return an INTEGER_ARRAY.
31
+ * The function accepts following parameters:
32
+ * 1. INTEGER_ARRAY a
33
+ * 2. INTEGER_ARRAY b
34
+ */
35
+
36
+ function compareTriplets ( a : number [ ] , b : number [ ] ) : number [ ] {
37
+ const score : number [ ] = [ ] ;
38
+ let pointOfAlice : number = 0 ;
39
+ let pointOfBob : number = 0 ;
40
+ // Alice and Bob
41
+ // input a = [1, 2, 3] , b = [3, 4, 1]
42
+ // comparison a[i] > b[i], a[i] < b[i] , a[i] == b[i]
43
+ // retrun array [1, 1] pointOfAlice and Bob score position
44
+
45
+ for ( let i = 0 ; i <= 2 ; i ++ ) {
46
+ if ( a [ i ] == b [ i ] ) {
47
+ pointOfAlice ;
48
+ pointOfBob ;
49
+ } else if ( a [ i ] > b [ i ] ) {
50
+ pointOfAlice += 1 ;
51
+ } else if ( a [ i ] < b [ i ] ) {
52
+ pointOfBob += 1 ;
53
+ }
54
+ }
55
+
56
+ return score . concat ( pointOfAlice , pointOfBob ) ;
57
+ }
58
+
59
+ function compareTripletsVariationOne ( a : number [ ] , b : number [ ] ) : number [ ] {
60
+ let pointOfAlice : number = 0 ;
61
+ let pointOfBob : number = 0 ;
62
+ // Alice and Bob
63
+ // input a = [1, 2, 3] , b = [3, 4, 1]
64
+ // comparison a[i] > b[i], a[i] < b[i] , a[i] == b[i]
65
+ // retrun array [1, 1] pointOfAlice and Bob score position
66
+
67
+ for ( let i = 0 ; i < a . length ; i ++ ) {
68
+ if ( a [ i ] > b [ i ] ) {
69
+ pointOfAlice += 1 ;
70
+ } else if ( a [ i ] < b [ i ] ) {
71
+ pointOfBob += 1 ;
72
+ }
73
+ }
74
+
75
+ return [ pointOfAlice , pointOfBob ] ;
76
+ }
0 commit comments