Advertisement
nikolayneykov92

Untitled

Jun 12th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution (params) {
  2.   return (function () {
  3.     const storage = { protein: 0, carbohydrate: 0, fat: 0, flavour: 0 }
  4.     const recipesLibrary = {
  5.       apple: { carbohydrate: 1, flavour: 2 },
  6.       lemonade: { carbohydrate: 10, flavour: 20 },
  7.       burger: { carbohydrate: 5, fat: 7, flavour: 3 },
  8.       eggs: { protein: 5, fat: 1, flavour: 1 },
  9.       turkey: { protein: 10, carbohydrate: 10, fat: 10, flavour: 10 }
  10.     }
  11.  
  12.     return input => {
  13.       let [command, ...args] = input.split(' ')
  14.       let message = ''
  15.  
  16.       if (command === 'restock') {
  17.         let [microelement, quantity] = [args[0], +args[1]]
  18.         storage[microelement] += quantity
  19.         message = 'Success'
  20.       } else if (command === 'prepare') {
  21.         let [recipe, quantity] = [args[0], +args[1]]
  22.         let missingIngredient = Object.entries(recipesLibrary[recipe]).find(
  23.           ([name, requiredQuantity]) =>
  24.             storage[name] < requiredQuantity * quantity
  25.         )
  26.  
  27.         if (missingIngredient) {
  28.           message = `Error: not enough ${missingIngredient[0]} in stock`
  29.         } else {
  30.           for (let ingredient in recipesLibrary[recipe]) {
  31.             storage[ingredient] -= recipesLibrary[recipe][ingredient] * quantity
  32.           }
  33.  
  34.           message = 'Success'
  35.         }
  36.       } else if (command === 'report') {
  37.         message =
  38.           `protein=${storage.protein} carbohydrate=${storage.carbohydrate}` +
  39.           ` fat=${storage.fat} flavour=${storage.flavour}`
  40.       }
  41.  
  42.       console.log(message)
  43.       return message
  44.     }
  45.   })()
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement