-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferenceOfCuboids.ts
More file actions
33 lines (27 loc) · 987 Bytes
/
Copy pathdifferenceOfCuboids.ts
File metadata and controls
33 lines (27 loc) · 987 Bytes
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
/*
In this simple exercise, you will create a program that will take two lists of integers, a and b. Each list will consist of 3 positive integers above 0, representing the dimensions of cuboids a and b. You must find the difference of the cuboids' volumes regardless of which is bigger.
For example, if the parameters passed are ([2, 2, 3], [5, 4, 1]), the volume of a is 12 and the volume of b is 20. Therefore, the function should return 8.
*/
function findDifference(a: number[], b: number[]) {
/*
let one = 1;
let two = 1;
for (let i = 0; i < a.length; i++) {
one *= a[i];
}
for (let i = 0; i < b.length; i++) {
two *= b[i];
}
let difference = one - two;
if (difference < 1) {
difference *= -1;
} else {
difference = difference;
}
*/
return Math.abs(
a.reduce((previous, current) => previous * current, 1) -
b.reduce((previous, current) => previous * current, 1)
);
}
console.log(findDifference([2, 2, 3], [5, 4, 1]));