Let's say you were given an array of object data from database by typing some query.
And you want to get some results of remained_deposit, remained_point, total_balance and refund_amount.
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 result 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 remain 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 remain 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 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 remain 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.
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.
If you can understand, then can you help me make some JS code to get the result?
I tried creating JS code to get the expected result, but I kept failing.. and it brought me to Stackoverflow.
Related
I have a maths problem I am struggling to get as accurate as I can when working out commission within a JavaScript application.
A user as part of this system has a commission threshold:
[
{
id: 123,
commissionThreshold: 3700
},
{
id: 456,
commissionThreshold: 3000
}
]
A user also has bookings within the system
[
{
id: 1,
user: 123,
commission: 20, // this is the percentage commission to be paid for this booking
bookingProfit: 4000
},
{
id: 2,
user: 123,
commission: 20, // this is the percentage commission to be paid for this booking
bookingProfit: 2000
},
{
id: 3,
user: 456,
commission: 20, // this is the percentage commission to be paid for this booking
bookingProfit: 3000
}
]
Commission is worked out by taking the overall booking profit for a users bookings, minus the users commission threshold from the booking profit. If that booking profit - threshold is greater than 0, commission is X percent of the booking profit - threshold. X percent is the commission value from the booking.
For the above dataset this is simple because all bookings have the same commission.
// user 1 has 2 bookings
// total booking profit for user 1 = 6000
// booking profit - threshold for user 1 = 2300
// commission for user 1 = 2300 / 0.2 = 460 // 0.2 is from all bookings have commission of 20%
// commission for user 1 = 460
// commission for user 2 would be 0
// booking profit = 3000
// commission booking profit - threshold < 0 so no commission is paid
The problem is some bookings in reality have different commissions so you can't add up all totals, then work out commission from that.
[
{
id: 1,
user: 123,
commission: 20, // this is the percentage commission to be paid for this booking
bookingProfit: 4000
},
{
id: 2,
user: 123,
commission: 15, // this is the percentage commission to be paid for this booking
bookingProfit: 2000
},
{
id: 2,
user: 123,
commission: 25, // this is the percentage commission to be paid for this booking
bookingProfit: 6000
},
]
In the example above where user 123 has 3 bookings all with different commission percentages how would I go about working out the total commission from all bookings because the commission threshold is the total for all bookings within the dataset not per booking?
I am stumped.
The solution I have at present is for each:
// calculate the total booking profit from all the users bookings
// then for each booking work out the percentage of the total booking profit the individual booking profit is
// get the same percentage of the commission threshold
// do the relative booking profit - relative threshold > 0 ? relative booking profit - relative threshold * booking commission percentage : 0a
//e.g.
// total booking profit for user 123 in above is 12000
// for booking 1
4000 / 12000 = 0.333333333333333
user threshold 3700 * 0.333333333333333 = 1233.3333333333321;
4000 - 1233.3333333333321 = 2766.6666666666679
2766.6666666666679 * 0.2 (booking percentage) = 553.33333333333358
// repeat for all bookings for each user
You could collect the parts and calculate the profit for each commission.
const
users = [{ id: 123, commissionThreshold: 3700 }, { id: 456, commissionThreshold: 3000 }],
bookings = [{ id: 1, user: 123, commission: 20, bookingProfit: 4000 }, { id: 2, user: 123, commission: 15, bookingProfit: 2000 }, { id: 2, user: 123, commission: 25, bookingProfit: 6000 }],
result = bookings.reduce(
(r, { user, commission, bookingProfit }) => {
r[user].grossProfit += bookingProfit;
r[user].netProfit += bookingProfit;
r[user].bookings.push({ bookingProfit, commission });
return r;
},
Object.fromEntries(users.map(({ id, commissionThreshold }) => [id, {
netProfit: -commissionThreshold,
grossProfit: 0,
totalProfit: 0,
bookings: []
}]))
);
Object.values(result).forEach(commission => {
if (commission.netProfit <= 0) return;
commission.bookings.forEach(booking => {
booking.part = commission.netProfit * booking.bookingProfit / commission.grossProfit;
booking.profit = booking.part * booking.commission / 100;
commission.totalProfit += booking.profit;
});
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have an array of tracks.
const history = [
{
entryDate: "2022-05-03T09:32:07.137+02:00",
type: "statusChange",
message: "",
},
{
entryDate: "2022-05-02T19:32:07.137+02:00",
type: "statusChange",
message: "",
},
{
entryDate: "2022-05-02T09:32:07.137+02:00",
type: "statusChange",
message: "",
},
{
entryDate: "2022-05-02T07:30:01.672+02:00",
type: "statusChange",
message: "",
}
];
How to get the number of minutes that was tracked only during given times, for example:
How many minutes was the user tracked from 09:30 - 18:00 from the given array of tracks?
Hard to figure it out myself.
Not sure how you know which elements in the array are “workstart” vs “workend” events. Assuming you have that part figured out, and that you have four date variables workStart, workEnd, trackStart, trackEnd, which are numeric:
let start = Math.max(workstart, trackstart);
let end = Math.min(workend, trackend);
let result = end > start ? end - start : 0;
An app lets users order food from a menu. The menu has three types of selection: main, drink and dessert. A feature needs to be added which will discount the price by 10% for every main+drink combo (10% off every combo). All items ordered by the customer are stored in an array like so:
order = [
{id: 4, count: 1, type: "main", price: 10}
{id: 5, count: 2, type: "drink", price: 9.5}
]
As you can see, each item the customer orders has a count property. How can I apply the discount without mutating the order array or any of the object properties? Ideally I'd like to loop through the array, determine total number of combos (in the example above it would be 1), determine the total discount value and pass that value to another function which computes the order total. If anyone can suggest a better way of doing it, I'm all ears (or eyes in this case).
Also, what is the best way to express this problem from a technical point of view?
const userOrder = [
{ id: 4, count: 1, type: "main", price: 200 },
{ id: 5, count: 1, type: "drink", price: 100 }
];
const orderInfo = userOrder.reduce((acc, cur) => {
console.log('cur', cur)
if (acc[cur.type]) {
return {
...acc,
[cur.type]: cur.count,
totalAmount: (cur.count * acc.totalAmount)
}
} else {
return {
...acc,
[cur.type]: cur.count,
totalAmount: (cur.count * cur.price ) + acc.totalAmount
}
}
}, {
main: 0,
drink: 0,
totalAmount: 0
});
const noOfComobosPresent = Math.min(orderInfo.main, orderInfo.drink);
const totalDiscountValue = noOfComobosPresent * 10;
const finalAmount = orderInfo.totalAmount - ((orderInfo.totalAmount * totalDiscountValue ) / 100) ;
console.log('finalAmount', finalAmount)
This is more of a general question of both code and math. I'm not at all good at math, and I'm still learning how to apply math in programming.
Let's say, I have an object of data that has the amount, the measurement, and the type, such as feet or lb.
const data = {
0: {
'type': 'imperial',
'unit': 'ft',
'amount': 3
},
1: {
'type': 'imperial',
'unit': 'lb',
'amount': 5
},
2: {
'type': 'imperial',
'unit': 'mph',
'amount': 7
}
}
And I need to go through this data and convert each according to the type (assuming type is what it's called)
Object.keys(data).map(key => {
convert(data[key]['amount'], data[key]['type'], data[key]['unit'])
})
And the function converts this:
const convert = (amount, type, unit) => {
const calc = // ???
return calc;
}
My question is, how do I convert depending on the type of measurement? I know that 1 foot is 0.3048 meters, and if I needed to convert 5 feet to meters, I'd do 5*0.3048.
However, how can I apply this in code, with a list of imperial and metric units and how would I add this to the convert function?
You can have a converter Object with functions to convert and labels to display, here's an example ( adjust the values and units to your needs ) :
const data = {
0: {
type: "imperial",
unit: "ft",
amount: 3
},
1: {
type: "imperial",
unit: "lb",
amount: 5
},
2: {
type: "imperial",
unit: "mph",
amount: 7
}
};
const converter = {
imperialToMetric: {
ft: val => val * 0.3048,
lb: val => val * 0.453592,
mph: val => val * 1.60934,
labels: {
ft: "meters",
lb: "Kg",
mph: "kmh"
}
},
metric: {
// reverse the above
}
};
const result = Object.values(data).map(({ amount, type, unit }) => ({
amount: converter.imperialToMetric[unit](amount),
unit: converter.imperialToMetric.labels[unit],
type: "metric"
}));
console.log(result);
Trying to create a hidden markov model to find recurring payments in this transactions json:
https://pastebin.com/tzRaqMxk
I created a similarity score, to estimate the likely hood of a transaction date, amount, and name being a recurring transaction.
nn = require('nearest-neighbor');
const items = https://pastebin.com/tzRaqMxk //pastebin json here
var query = { amount: 89.4, name: "SparkFun", date: "2017-05-28"};
var fields = [
{ name: "name", measure: nn.comparisonMethods.word },
{ name: "amount", measure: nn.comparisonMethods.number, max: 100 },
{ name: "date", measure: nn.comparisonMethods.date, max: 31 }
];
nn.findMostSimilar(query, items, fields, function(nearestNeighbor, probability) {
console.log(query);
console.log(nearestNeighbor);
console.log(probability);
});
The first challenge is what to do if the recurring transaction is not on the same day of the month I.e. usually happens on the 18th, but because the 18th fell on a Saturday, the payment doesn't clear until the 20th. What statistical measure to I used to identify a similar score as nearly the same, but not a probability of 1.
Then after I have this array of data, how can I feed that into a hidden markov model?