Skip to content

Commit 082857d

Browse files
add: getting-the-distinct-transactions
1 parent 1fd4b7b commit 082857d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
- [Filling all the missed angle brackets](filling-all-the-missed-angle-brackets)
44
- [Finding all the anagrams](finding-all-the-anagrams)
55
- [Finding the maximum depth of a binary tree](finding-the-maximum-depth-of-a-binary-tree)
6+
- [Getting the distinct transactions](getting-the-distinct-transactions)
67
- [Replacing with the cipher letters](replacing-with-the-cipher-letters)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Getting the distinct transactions
2+
3+
Fix the below code for only the distinct transactions
4+
5+
```javascript
6+
const solution = (transactions, taxRate) => {
7+
let numCalls = 0;
8+
9+
const calculateCostAfterTax = (cost, taxRate) => {
10+
numCalls = numCalls + 1;
11+
return cost * taxRate;
12+
};
13+
14+
const computeTotal = (taxRate) => {
15+
return (cost) => {
16+
return calculateCostAfterTax(cost, taxRate);
17+
};
18+
};
19+
20+
transactions.map(computeTotal(taxRate));
21+
22+
return numCalls;
23+
};
24+
```
25+
26+
e.g.:
27+
28+
Input
29+
30+
```console
31+
transactions = [3, 4, 3, 7, 4, 9]
32+
taxRate = 2.5
33+
```
34+
35+
Output
36+
37+
```console
38+
4
39+
```
40+
41+
## Execute
42+
43+
```bash
44+
node solution.js
45+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const solution = (transactions, taxRate) => {
2+
let numCalls = 0;
3+
4+
const calculateCostAfterTax = (cost, taxRate) => {
5+
numCalls = numCalls + 1;
6+
return cost * taxRate;
7+
};
8+
9+
const computeTotal = (taxRate) => {
10+
return (cost) => {
11+
return calculateCostAfterTax(cost, taxRate);
12+
};
13+
};
14+
15+
const distinctTransactions = new Set(transactions);
16+
17+
Array.from(distinctTransactions).map(
18+
computeTotal(taxRate),
19+
);
20+
21+
return numCalls;
22+
};
23+
24+
(() => {
25+
console.log(solution([3, 4, 3, 7, 4, 9], 2.5));
26+
})();

0 commit comments

Comments
 (0)