|
| 1 | +//001. sum of two integers |
| 2 | +function solveMeFirst(a: number, b: number) { |
| 3 | + // Hint: Type return a+b below |
| 4 | + return a + b; |
| 5 | +} |
| 6 | +// 002. Sum of an Array |
| 7 | +// Calculate the Sum of an Array Using the reduce() Method in JavaScript |
| 8 | +/* |
| 9 | + * Complete the 'simpleArraySum' function below. |
| 10 | + * |
| 11 | + * The function is expected to return an INTEGER. |
| 12 | + * The function accepts INTEGER_ARRAY ar as parameter. |
| 13 | + */ |
| 14 | + |
| 15 | +function simpleArraySum(ar: number[]): number { |
| 16 | + // Write your code here |
| 17 | + // parameter 'ar' ar: an array of integers |
| 18 | + const sum: number = ar.reduce((accumulator, currentValue) => { |
| 19 | + return accumulator + currentValue; |
| 20 | + }, 0); |
| 21 | + // Return the sum of the array |
| 22 | + return sum; |
| 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