Skip to content

Instantly share code, notes, and snippets.

@epoch
Forked from micaelsbno/api_banquet.md
Created November 28, 2018 11:55
Show Gist options
  • Save epoch/77a4e4a12748182fcb75c60567146a79 to your computer and use it in GitHub Desktop.
Save epoch/77a4e4a12748182fcb75c60567146a79 to your computer and use it in GitHub Desktop.

API Banquet

DT and Kasun are planning to make a party. DT used goodfoodhunting to fetch recipes and prepare a banquet and is sending Kasun the data can cook everything in his automatic cooking machine.

Here's the data that DT got:

dishes = [
  {
    name: 'parma',
    index: 1,
    ingredients: ['chicken', 'cheese', 'tomato'],
    quantity: 3
    table: {name: 'mains', id: 1, tableIndex: 2}
  },
  {
    name: 'spring rolls',
    index: 1,
    ingredients: ['shiitake', 'rice paper rolls', 'spring onion'],
    quantity: 12
    table: {name: 'starters', id: 2, tableIndex: 1}
  },
  {
    name: 'chicken wings',
    index: 2,
    ingredients: ['chicken wing', 'hot sauce', 'blue cheese'],
    quantity: 14
    table: {name: 'starters', id: 2, tableIndex: 1},
  }

  {
    name: 'steak',
    index: 2,
    ingredients: ['steak', 'chips', 'salad'],
    quantity: 7
    table: {name: 'mains', id: 1, tableIndex: 2}
  },
  {
    name: 'pudding',
    index: 1,
    ingredients: ['milk', 'eggs', 'caramel'],
    quantity: 20,
    table: {name: 'desserts', id: 3, tableIndex: 3},
  },
  {
    name: 'ice cream',
    index: 2,
    ingredients: ['milk', 'vanilla'],
    quantity: 10
    table: {name: 'desserts', id: 3, tableIndex: 3},
  },
  {
    name: 'cake',
    index: 3,
    ingredients: ['milk', 'flour', 'chocolate'],
    quantity: 1,
    table: {name: 'desserts', id: 3, tableIndex: 3}
  }
]
The problem is that the machine only works if the data is correctly ordered within it's standards. Which can be seen below:
var banquet = {
  dishes: {
    'name': { quantity: 0, ingredients: [] }
  },
  tables: {
    'table': {
      name: '',
      dishOrder: ['dish1', 'dish2']
    }
  }
  tableOrder: ['tableName1', 'tableName2']
}

Make a function that will accept an array of dishes and will output an object that fits the machine's standard and save everyone's lunch

Extension: make a function that will add a new dish
Extension 2: make a function that will allow user to change a dish order

Expected Output:

organizeDishes(dishes) => 

{
  dishes: {
    'parma': { quantity: 3, ingredients: ['chicken', 'cheese', 'tomato'] },
    'spring rolls': { quantity: 12, ingredients: ['shiitake', 'rice paper rolls', 'spring onion'] },
    'steak': { quantity: 7, ingredients: ['steak', 'chips', 'salad'] },
    'chicken wings': { quantity: 14, ingredients: ['chicken wings', 'hot sauce', 'blue cheese'] },
    'pudding': { quantity: 20, ingredients: ['milk', 'eggs', 'vanilla'] },
    'ice cream': { quantity: 10, ingredients: ['milk', 'vanilla'] },
    'cake': { quantity: 1, ingredients: ['milk', 'flour', 'chocolate'] }
  },
  tables: {
    'mains': {
      id: 1,
      dishOrder: ['parma', 'steak']
    },
    'desserts': {
      id: 3,
      dishOrder: ['pudding', 'ice cream', 'cake']
    },
    'starters': {
      id: 1,
      dishOrder: ['spring rolls', 'chicken wings']
    }
  },
  tableOrder: ['starters', 'mains', 'desserts']
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment