Skip to content

Instantly share code, notes, and snippets.

@nwingt
Last active April 23, 2021 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwingt/995c3f51fa633fffc98b4ac24777ddca to your computer and use it in GitHub Desktop.
Save nwingt/995c3f51fa633fffc98b4ac24777ddca to your computer and use it in GitHub Desktop.
POS Sample Project
// 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