├── index.html
└── src
└── js
└── index.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 | HW12-js-function-constructor
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/js/index.js:
--------------------------------------------------------------------------------
1 | console.log('------------- #3')
2 | const SIZE_SMALL = 'SIZE_SMALL'
3 | const SIZE_MIDDLE = 'SIZE_MIDDLE'
4 | const SIZE_BIG = 'SIZE_BIG'
5 |
6 | const CHEESE = 'CHEESE'
7 | const SALAD = 'SALAD'
8 | const POTATO = 'POTATO'
9 | const FLAVORING = 'FLAVORING'
10 | const TOPPING_MAYO = 'TOPPING_MAYO'
11 |
12 |
13 | function Hamburger(size) {
14 | this.ingredients = []
15 | switch (size) {
16 | case 'SIZE_SMALL':
17 | this.price = 50
18 | this.calories = 20
19 | break
20 | case 'SIZE_MIDDLE':
21 | this.price = 75
22 | this.calories = 30
23 | break
24 | case 'SIZE_BIG':
25 | this.price = 100
26 | this.calories = 40
27 | break
28 | default:
29 | notAvailableValue('size', size)
30 | }
31 | this.addModifier = function (ingredient) {
32 | switch (ingredient) {
33 | case 'CHEESE':
34 | this.price += 10
35 | this.calories += 20
36 | this.ingredients.push(ingredient)
37 | break
38 | case 'SALAD':
39 | this.price += 20
40 | this.calories += 5
41 | this.ingredients.push(ingredient)
42 | break
43 | case 'POTATO':
44 | this.price += 15
45 | this.calories += 10
46 | this.ingredients.push(ingredient)
47 | break
48 | case 'FLAVORING':
49 | this.price += 15
50 | this.ingredients.push(ingredient)
51 | break
52 | case 'TOPPING_MAYO':
53 | this.price += 20
54 | this.calories += 5
55 | this.ingredients.push(ingredient)
56 | break
57 | default:
58 | notAvailableValue('ingredient', ingredient)
59 | }
60 | }
61 | this.info = function () {
62 | console.log(`Burger ${size.toLowerCase()} with ${this.ingredients.map(el => el.toLowerCase()).join(', ')}`)
63 | console.log(`Price: ${this.getPrice()}`)
64 | console.log(`Calories: ${this.getCalories()}`)
65 | }
66 | this.getPrice = function () {
67 | return this.price ? `${this.price} tugriks` : 'Invalid hamburger'
68 | }
69 | this.getCalories = function () {
70 | return this.calories ? `${this.calories} calories` : 'Invalid hamburger'
71 | }
72 | }
73 | function notAvailableValue(category, val) {
74 | console.error(`${category} "${val}" is not available for hamburger`)
75 | }
76 |
77 | const hamburger = new Hamburger(SIZE_SMALL)
78 | hamburger.addModifier(TOPPING_MAYO)
79 | hamburger.addModifier(POTATO)
80 | hamburger.info()
81 |
--------------------------------------------------------------------------------