Created
July 27, 2023 11:19
-
-
Save erhanyasar/9c6b7c9a510f01b4e07653dfedd0b0a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given Brief | |
# Find a total ordered quantity of a product for every weekday | |
You are asked to improve the analytics of an e-commerce platform: calculating a total quantity of a given product ordered should be added. | |
# Task | |
Implement `OrdersAnalyzer.totalQuantity(orders, productId)` method which | |
1. For given order collections, it calculates **total quantity of a given product ordered, grouped by days of weeks**. | |
2. The result is an object which includes names of all days of the week as a keys, and a sum of ordered quantity for a given product id as a value. | |
3. The method should always return an object. If there are no orders for passed `productId` for some weekday, there might be no entry in the resulting object or there might be an entry with a `0` value. | |
## Example | |
### Example data | |
The _orders_ collection is available in `data.json`: | |
``` | |
[ | |
{ | |
orderId: 554, | |
creationDate: "2017-03-25T10:35:20", // Saturday | |
orderLines: [ | |
{productId: 9872, name: 'Pencil', quantity: 3, unitPrice: 3.00} | |
] | |
}, | |
{ | |
orderId: 555, | |
creationDate: "2017-03-25T11:24:20", // Saturday | |
orderLines: [ | |
{productId: 9872, name: 'Pencil', quantity: 1, unitPrice: 3.00}, | |
{productId: 1746, name: 'Eraser', quantity: 1, unitPrice: 1.00} | |
] | |
}, | |
{ | |
orderId: 453, | |
creationDate: "2017-03-27T14:53:12", // Monday | |
orderLines: [ | |
{productId: 5723, name: 'Pen', quantity: 4, unitPrice: 4.22}, | |
{productId: 9872, name: 'Pencil', quantity: 3, unitPrice: 3.12}, | |
{productId: 3433, name: 'Erasers Set', quantity: 1, unitPrice: 6.15} | |
] | |
}, | |
{ | |
orderId: 431, | |
creationDate: "2017-03-20T12:15:02", // Monday | |
orderLines: [ | |
{productId: 5723, name: 'Pen', quantity: 7, unitPrice: 4.22}, | |
{productId: 3433, name: 'Erasers Set', quantity: 2, unitPrice: 6.15} | |
] | |
}, | |
{ | |
orderId: 690, | |
creationDate: "2017-03-26T11:14:00", // Sunday | |
orderLines: [ | |
{productId: 9872, name: 'Pencil', quantity: 4, unitPrice: 3.12}, | |
{productId: 4098, name: 'Marker', quantity: 5, unitPrice: 4.50} | |
] | |
} | |
]; | |
``` | |
### Example usage | |
```js | |
const analyzer = new OrdersAnalyzer(); | |
analyzer.analyzer.totalQuantity(orders, 9872); | |
``` | |
## Example output | |
Your implementation should return following output for **product id=9872** for example data: | |
```json | |
{ | |
"MONDAY" : 3, | |
"TUESDAY" : 0, | |
"WEDNESDAY" : 0, | |
"THURSDAY" : 0, | |
"FRIDAY " : 0, | |
"SATURDAY" : 4, | |
"SUNDAY" : 4 | |
} | |
``` | |
In the example above, there are: | |
- two orders placed on **Saturday** | |
- first with quantity equal to `3` (id:`554`) and second with quantity equal to `1` (id:`555`), so the sum of quantities is `4`. | |
- one order placed on **Sunday** | |
- so the total quantity is `4`. | |
- two orders placed on **Monday** | |
- and only one of them (id:`453`) includes this product with quantity equal to `3`. So total quantity is also `3`. | |
### Testcases | |
Testcases are defined in `testcases.json`. They include all days of week, like the following: | |
```json | |
{ | |
"1746": { | |
"SUNDAY": 0, | |
"MONDAY": 0, | |
"TUESDAY": 0, | |
"WEDNESDAY": 0, | |
"THURSDAY": 0, | |
"FRIDAY": 0, | |
"SATURDAY": 1 | |
}, | |
"3433": { | |
"SUNDAY": 0, | |
"MONDAY": 3, | |
"TUESDAY": 0, | |
"WEDNESDAY": 0, | |
"THURSDAY": 0, | |
"FRIDAY": 0, | |
"SATURDAY": 0 | |
}, | |
... | |
} | |
``` | |
This means that for a given dataset, there would be tests for each day-of-week AND product combination: | |
- test scenario 1: product id:1746, all days of week | |
- test scenario 2: product id:3433, all days of week | |
*/ | |
export default class OrdersAnalyzer { | |
constructor() { | |
this.weekdays = [ | |
"SUNDAY", | |
"MONDAY", | |
"TUESDAY", | |
"WEDNESDAY", | |
"THURSDAY", | |
"FRIDAY", | |
"SATURDAY", | |
]; | |
} | |
totalQuantity(orders, productId) { | |
let weeklyQuantities = Array(7).fill(0); | |
orders.forEach((order) => { | |
const orderDate = new Date(order.creationDate); | |
let dayOfWeek = orderDate.getDay(); | |
order.orderLines?.forEach((orderLine) => { | |
if (productId === orderLine.productId) | |
weeklyQuantities[dayOfWeek] += orderLine.quantity; | |
}); | |
}); | |
return { | |
SUNDAY: weeklyQuantities[0], | |
MONDAY: weeklyQuantities[1], | |
TUESDAY: weeklyQuantities[2], | |
WEDNESDAY: weeklyQuantities[3], | |
THURSDAY: weeklyQuantities[4], | |
FRIDAY: weeklyQuantities[5], | |
SATURDAY: weeklyQuantities[6], | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment