Let’s say you were given an array of object data from database and you want to get REFUND result.
So you can give the amount of money when a user requests refund. Before we dive into the problem
- 'DEPOSIT' is the money that user deposit
- 'POINT' is like an additional extra money/deposit that the business provide when the amount of deposit is over $30($30 deposit => 3 POINT is given, $50 deposit => 10 POINT is given )
- 'POINT' is not refundable.
- 'DEDUCTION' is the amount of money that user spent.
- THE RULE IS YOU NEED TO SUBTRACT DEDUCTION FROM DEPOSIT FIRST. AND WHEN DEPOSIT BECOMES 0 or NEGATIVE, WE SUBTRACT DEDUCTION FROM POINT (if exists..) - you can realize what it means in the example 2
Let’s say I got user 1’s deposit history data by typing query.
const queryResult = [
{
date: '2022-10-10',
amount : 50,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 10,
type: 'POINT',
},
{
date: '2022-10-10',
amount : -5,
type: 'DEDUCTION',
},
{
date: '2022-10-10',
amount : -5,
type: 'DEDUCTION',
},
];
In this case, the result must become
const result = {
remained_deposit: 40,
remained_point: 10,
balance: 50 (remained_deposit + remained_point),
refundable: 40(remained_deposit)
}
** explain: we have 50 deposit and subtract two deduction from deposit.
(50 -5 -5 = 40)
So, when the user requests refund, the user must get refund $40 (not $50..)
Let’s see another case.
const data2 = [
{
date: '2022-10-10 ',
amount : 50,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 10,
type: 'POINT',
},
{
date: '2022-10-10',
amount : -30,
type: 'DEDUCTION',
},
{
date: '2022-10-11',
amount : -25,
type: 'DEDUCTION',
},
{
date: '2022-10-11',
amount : 10,
type: 'DEPOSIT',
},
];
In this case, the result must become
const result2 = {
remained_deposit: 10,
remained_point: 5,
balance: 15,
refundable: 10
}
As we did above, we subtract deduction from deposit first.
50(deposit) - 30(deduction) = 20 (deposit)
20(deposit) - 25(deduction) = -5(deposit.. becomes negative)
In this case, we calculate with the POINT.
-5(deposit) + 10 (POINT) = 5(POINT)
if the user requests refund at this point, he/she can get nothing.
Because remained balance which is POINT(5) is not refundable.
However since the user deposit 10 after the last deduction,
and if he/she requests refund at this point, remained_deposit 10 will be given to the user.
Let’s see one more confusing-looking case
const data3 = [
{
date: '2022-10-10',
amount : 50,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 10,
type: 'POINT',
},
{
date: '2022-10-10',
amount : -40,
type: 'DEDUCTION',
},
{
date: '2022-10-11',
amount : 30,
type: 'DEPOSIT',
},
{
date: '2022-10-11',
amount : 3,
type: 'POINT',
},
{
date: '2022-10-11',
amount : -25,
type: 'DEDUCTION',
},
];
In this case, the result must become
const result3 = {
remained_deposit: 25,
remained_point: 3,
balance: 28,
refundable: 25
}
**explanation
50(deposit) - 40(deduction) = 10(deposit)
10(deposit) - 25(deduction) = -15(deposit)
**even though deposit(30) comes first than deduction(-25), we subtract deduction from the very first deposit first.
-15(deposit) + 10(point ) = -5(deposit)
-5(deposit) + 30(deposit) = 25(deposit)
What matters is that the first deposit needs to be whole subtracted first and then POINT is next.
When the user request refund in this case, he/she can get $25.
I hope the examples gave you some clues how you guys must calculate to get the refund.
If you are still confused, please let me know.
And if you guys can understand the problem, I would appreciate if you guys can advise me or give me a better clues, algorithms or solutions.
Thank you.
My JS Code Solution
let totalBalance = 0;
let totalRefund = 0;
// Get total balance
for(let result of results){
totalBalance += result.amount
}
console.log(totalBalance);
// Get total refund --it gets correct answer when it's case4(below), but it's wrong in case5
for(let result of results){
totalRefund += result.amount
// console.log(totalRefund);
if(result.type === 'POINT'){
totalRefund -= result.amount
}
if(totalRefund < 0){
totalRefund = 0;
}
}
// in case 5, the totalRefund supposed to be 15. But its result is 10...
case4
const results = [
{
date: '2022-10-10',
amount : 50,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 10,
type: 'POINT',
},
{
date: '2022-10-10',
amount : -45,
type: 'DEDUCTION',
},
{
date: '2022-10-10',
amount : -10,
type: 'DEDUCTION',
},
{
date: '2022-10-11',
amount : 30,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 3,
type: 'POINT',
},
]
// totalRefund = 30 (correct)
50(deposit) - 45(deduction) = 5(deposit)
5(deposit) - 10(deduction) = -5(deposit)
-5(deposit) + 10(point) = 5(point)
the remain_deposit is 30 and remained point is = 8(5+3)
case5
const results = [
{
date: '2022-10-10',
amount : 50,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 10,
type: 'POINT',
},
{
date: '2022-10-10',
amount : -45,
type: 'DEDUCTION',
},
{
date: '2022-10-10',
amount : -10,
type: 'DEDUCTION',
},
{
date: '2022-10-11',
amount : 30,
type: 'DEPOSIT',
},
{
date: '2022-10-10',
amount : 3,
type: 'POINT',
},
{
date: '2022-10-10',
amount : -10,
type: 'DEDUCTION',
},
{
date: '2022-10-10',
amount : -10,
type: 'DEDUCTION',
},
]
// totalRefund = 10 (wrong)
50(deposit) - 45(deduction) = 5(deposit)
5(deposit) - 10(deduction) = -5(deposit)
-5(deposit) + 10(point) = 5(point)
5(point) - 10(deduction) = -5(deduction)
-5(deduction) + 30(deposit) = 25(deposit)
25(deposit) - 10(deduction) = **15(remained_deposit == refund)**
Googling and code by myself. But still get unwanted/unexpected result.
Some case, the code can get the refund correctly, but in some cases it doesn’t.

