Skip to content

Commit 253f7e7

Browse files
author
wakidurrahman
committed
feat: problem solve
1 parent 987439d commit 253f7e7

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 004. Sum of an Array
2+
3+
/*
4+
* Complete the 'aVeryBigSum' function below.
5+
*
6+
* The function is expected to return a LONG_INTEGER.
7+
* The function accepts LONG_INTEGER_ARRAY ar as parameter.
8+
*/
9+
10+
function aVeryBigSum(ar) {
11+
// Write your code here
12+
const sum = ar.reduce((acct, curr) => {
13+
return acct + curr;
14+
}, 0);
15+
return sum;
16+
}
17+
18+
// 004:
19+
20+
function diagonalDifference(arr) {
21+
const lengthOfArray = arr.length; // Length of the array
22+
let leftToRight = 0;
23+
let rightToLeft = 0;
24+
25+
for (let i = 0; i < lengthOfArray; i++) {
26+
for (let j = 0; j < lengthOfArray; j++) {
27+
// Find The left-to-right diagonal
28+
if (i === j) {
29+
leftToRight += arr[i][j];
30+
}
31+
// Find The right to left diagonal diagonal
32+
if (i + j === lengthOfArray - 1) {
33+
rightToLeft += arr[i][j];
34+
}
35+
}
36+
}
37+
38+
// calculate the absolute difference between the sums of its diagonals.
39+
return Math.abs(leftToRight - rightToLeft);
40+
}
41+
42+
const myArray = [
43+
[0, 1, 2, 3],
44+
[4, 5, 6, 7],
45+
[8, 9, 0, 0],
46+
[4, 5, 6, 7],
47+
];
48+
diagonalDifference(myArray);

0 commit comments

Comments
 (0)