View difference between Paste ID: kWgAqTuE and 03zZidKa
SHOW: | | - or go back to the newest paste.
1
function solve() {
2
  const recipes = {
3
    apple: {
4
      carbohydrate: 1,
5
      flavour: 2
6
    }, 
7
    lemonade: {
8
      carbohydrate: 10,
9
      flavour: 20
10
    },
11
    burger: {
12
      carbohydrate: 5,
13
      fat: 7,
14
      flavour: 3
15
    },
16
    eggs: {
17
      protein: 5,
18
      fat:1,
19
      flavour: 1
20
    },
21
    turkey: {
22
      protein:10,
23
      fat: 10,
24
      carbohydrates: 10,
25
      flavour: 10,
26
    }
27
  }
28
29
  const stocks = {
30
    protein: 0,
31
    carbohydrate: 0,
32
    fat: 0,
33
    flavour: 0
34
  };
35
36
  const commands = {
37
    restock : (microelement, quantity) =>  {
38
      stocks[microelement] += quantity
39
      return 'Success';
40
    },
41
    prepare: (product, quanity) => { 
42
      let recipe = Object.entries(recipes[product]);
43
      
44
      for(let[item,countNeeded] of recipe) {
45
          if(stocks[item] < countNeeded * quanity) {
46
            return `Error: not enough ${item} in stock`;
47
          }
48
      }
49
50
      recipe.forEach(([item, countNeeded]) => {
51
        stocks[item] -= countNeeded * quanity;
52
      });
53
54
      return 'Success'
55
56
    },
57
    report : () => Object.
58
                    entries(stocks).
59
                    map(([microelement, count]) => `${microelement}=${count}` ).
60
                    join(' ')
61
  
62
  };
63
64
  return (input) => {
65
    let [command, item, count] = input.split(' ');
66
    return commands[command](item, +count);
67
  }
68
}