Last active
April 23, 2021 05:02
-
-
Save nwingt/995c3f51fa633fffc98b4ac24777ddca to your computer and use it in GitHub Desktop.
POS Sample Project
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
// Define very hea database | |
const foodDatabase = { | |
"π": { id: "π", type: "main", price: 25 }, | |
"π₯ͺ": { id: "π₯ͺ", type: "main", price: 20 }, | |
"π": { id: "π", type: "main", price: 30 }, | |
"π₯": { id: "π₯", type: "side", price: 20 }, | |
"π": { id: "π", type: "side", price: 15 }, | |
"π₯": { id: "π₯", type: "drink", price: 12 }, | |
"π₯€": { id: "π₯€", type: "drink", price: 10 }, | |
"π§": { id: "π§", type: "drink", price: 20 } | |
} | |
console.log('Defined food database:\n', foodDatabase) | |
function compareFoodByPrice(foodA, foodB) { | |
return foodB.price - foodA.price | |
} | |
function processFoodItems(input) { | |
// Query food items from input ID | |
const foodItems = [] | |
for (let i = 0; i < input.length; i++) { | |
const foodID = input[i] | |
const food = foodDatabase[foodID] | |
foodItems.push(food) | |
} | |
const mainItems = [] | |
const sideItems = [] | |
const drinkItems = [] | |
for (let i = 0; i < foodItems.length; i++) { | |
const food = foodItems[i]; | |
switch (food.type) { | |
case 'main': | |
mainItems.push(food) | |
break | |
case 'side': | |
sideItems.push(food) | |
break | |
case 'drink': | |
drinkItems.push(food) | |
break | |
} | |
} | |
mainItems.sort(compareFoodByPrice) | |
sideItems.sort(compareFoodByPrice) | |
drinkItems.sort(compareFoodByPrice) | |
console.log('\n\nStep 1: Sort grouped items by price in DESC order:') | |
console.log('\nMain Items:') | |
console.log(mainItems) | |
console.log('\nSide Items:') | |
console.log(sideItems) | |
console.log('\nDrink Items:') | |
console.log(drinkItems) | |
const maxNumberOfSets = Math.min(mainItems.length, sideItems.length, drinkItems.length) | |
console.log('\nAlso, we found the possible maximum number of sets (pick the minimum length of arrays from above):') | |
console.log(maxNumberOfSets) // 1 | |
const sets = [] | |
const canFormSets = maxNumberOfSets > 0 | |
if (canFormSets) { | |
for (let i = 0; i < maxNumberOfSets; i++) { | |
if (mainItems[0] && sideItems[0] && drinkItems[0]) { | |
// Remove the first item of the corresponding type of food items if exist | |
const main = mainItems.shift() | |
const side = sideItems.shift() | |
const drink = drinkItems.shift() | |
console.log('\nFound a set: ', main.id, side.id, drink.id) | |
// Form and add set to the result if there are main, side and drink | |
sets.push([main, side, drink]) | |
} | |
} | |
} | |
console.log('\n\nStep 3: Combined food items into sets:\n', sets) | |
// Put the rest of the food items which can't form a set to the result | |
const remainingItems = mainItems.concat(sideItems).concat(drinkItems) | |
console.log('\n\nStep 4: Remaining food items:\n', remainingItems) | |
return sets.concat(remainingItems) | |
} | |
const result1 = processFoodItems(["π", "π₯", "π", "π"]) | |
console.log('\n\nResult 1:') | |
console.log(result1) | |
/** | |
* [ | |
* [ | |
* { 'id': 'π', 'type': 'main', 'price': 25 }, | |
* { 'id': 'π', 'type': 'side', 'price': 15 }, | |
* { 'id': 'π₯', 'type': 'drink' 'price': 12 }, | |
* ], | |
* { 'id': 'π', 'type': 'side', 'price': 15 }, | |
* ] | |
*/ | |
const result2 = processFoodItems(["π", "π₯"]) | |
console.log('\n\nResult 2:') | |
console.log(result2) | |
/** | |
* [ | |
* { id: 'π', type: 'side', price: 15 }, | |
* { id: 'π₯', type: 'drink', price: 12 } | |
* ] | |
*/ | |
const result3 = processFoodItems(["π₯", "π₯", "π", "π", "π"]) | |
console.log('\n\nResult 3:') | |
console.log(result3) | |
/** | |
* [ | |
* [ | |
* { id: 'π', type: 'main', price: 30 }, | |
* { id: 'π₯', type: 'side', price: 20 }, | |
* { id: 'π₯', type: 'drink', price: 12 } | |
* ], | |
* { id: 'π', type: 'main', price: 25 }, | |
* { id: 'π', type: 'side', price: 15 } | |
* ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment