JSFiddle - React, Tailwind, and code Playground
JavaScript
var BIG_BURGER = 'big_burger';
var SMALL_BURGER = 'small_burger';
var CHEESE = 'cheese';
var SALAD = 'salad';
var POTATO = 'potato';
var burger_type = {
'big_burger' : {'price' : 100, 'nutrients' : 40},
'small_burger' : {'price' : 50, 'nutrients' : 20}
};
var stuffing_type = {
'cheese' : {'price' : 10, 'nutrients' : 20},
'salad' : {'price' : 20, 'nutrients' : 5},
'potato' : {'price' : 15, 'nutrients' : 10}
};
function Hamburger(type, stuffing){
if(!burger_type.hasOwnProperty(type) || !stuffing_type.hasOwnProperty(stuffing)){
throw 'Evil burger';
}
this.type = type;
this.stuffing = stuffing;
this.price = 0;
this.nutrients = 0;
this.add_flavoring = function(){
this.price += 15;
};
this.add_sauce = function(){
this.price += 20;
this.nutrients += 5
};
this.calc_price = function(){
return this.price + burger_type[this.type].price + stuffing_type[this.stuffing].nutrients;
};
this.calc_nutrients = function(){
return this.nutrients + burger_type[this.type].nutrients + stuffing_type[this.stuffing].nutrients;
};
}
var new_burger = new Hamburger(BIG_BURGER, CHEESE);
console.log(new_burger.calc_nutrients());
console.log(new_burger.calc_price());