JavaScript Moussaka Recipe

Cooking and programming. The two seems to have nothing in common, but I think they are very similar. There are many aspects that can be transferred and applied from one of the fields to the other.
Let’s take a look at recipes for example, they are very similar to computer programs. They both provide a set of instructions, which when followed corectly achieve something as a result. The only difference is that recipes are written in plain language such as English for example which is understandable to humans and computer programs are written in languages that computers can understand.

Lets take a look at a simple example of a recipe for a traditional Balkan dish – Moussaka, written in JavaScript. Along the way I will use some basic concepts of the language such as variables, objects and functions.

Let’s start with the first part of each recipe – the ingredients. If we have to translate that in a programming way, it would be a list of related items. A way to do that in JavaScript would be to store them in an object. In that way, we can group them together and later on reference them through the object.

//Ingredients Object
var ingredients{
	main : {
		onions : "2 Onions", 
		potatoes : "6 Potatoes",
		carrots : "2 Carrots",
		porkMince : "300gr Pork Mince",
		beefMince : "300gr Beef Mince",
		cannedTomatoes : "1 Can of Canned Tomatoes",
		water : "500ml boiling water"
	},
	overlay : {
		greekYogurt : "300ml Greek Yogurt",
		eggs : "2 Eggs",
		flour : "2 table spoons of flour"
	},
	spices : {
		oliveOil : "50ml Olive Oil",
		oil : "50ml Sunflower Oil",
		cummin : "2 table spoons of cummin",
		blackPepper : "1 table spoon of black papper", 
		paprika : "2 table spoons of paprika",
		grillSpices : "1 table spoon of grill spices mix",
		salt : "2 table spoons of salt",
		vegetableCube : "1 Vegetable Cube",
		parsley : "1 Pack of Parsley" 
	}
}

As you can see objects are used to store keyed collections of various data. There are key : value pairs, which allow you to reference the value of the keyed variables later on in the program(recipe).

The next concept needed for the execution of this recipe is functions. Functions are a way of packaging functionality that can be reused. For example, if hypothetically you give instructions to a computer of what an onion is and how it is chopped you can call that function over and over again and the computer will know what to do and return the same result every time. Here are the functions needed for the execution of the ‘recipe’:

//Functions

/**
 * Chops an igredient 
 * @param ingedient 
 * @return Chopped ingredient
 */
 chop(ingredient){
	 return ingredient / 15;
 }
 
 /**
 * Blends igredients 
 * @param {...*} ingredients
 * @return Blended ingredients
 */
 blend(...ingredients){
	return ingredients / 1000;
 }
 
 /**
 * Cooks an igredient 
 * @param ingedient 
 * @return Cooked ingredient
 */
 cook(ingredient){
	 return Cooked Ingredient;
 }

 /**
 * Mixes igredients
 * @param {...*} ingredients
 * @return Mixed Ingredients
 */
 mix(...ingredients){
	return Mixed Ingredients;
 }
 
 /**
 * Bakes igredients
 * @param {...*} ingredients
 * @param {Number} temperature (in celsius)
 * @param {Number} time (in minutes)
 * @return Baked Ingredients
 */
 bake(...igredients, time, temperature){
	 return Baked Ingredients;
 }
 
 /**
 * Serve the dish
 * @param readyDish
 * @param decoration
 * @param {Number} servings;
 * @return Serving in a plate
 */
 serve(readyDish, decoration, servings){
	 return (readyDish + decoration) / servings
 }

Unfortunatelly, the functions above lean more towards psudo code, but as an example they do the job. There are a few notable concepts here though that are fundamental and additional information about them is available in the links below:
Functions
Default parameters
Rest parameters

Once we have the ingredients and the functions ready, the only thing left is to logically structure our recipe.

Here is the full set of instructions:

//Ingredients Object
var ingredients{
	main : {
		onions : "2 Onions", 
		potatoes : "6 Potatoes",
		carrots : "2 Carrots",
		porkMince : "300gr Pork Mince",
		beefMince : "300gr Beef Mince",
		cannedTomatoes : "1 Can of Canned Tomatoes",
		water : "500ml boiling water"
	},
	overlay : {
		greekYogurt : "300ml Greek Yogurt",
		eggs : "2 Eggs",
		flour : "2 table spoons of flour"
	},
	spices : {
		oliveOil : "50ml Olive Oil",
		oil : "50ml Sunflower Oil",
		cummin : "2 table spoons of cummin",
		blackPepper : "1 table spoon of black papper", 
		paprika : "2 table spoons of paprika",
		grillSpices : "1 table spoon of grill spices mix",
		salt : "2 table spoons of salt",
		vegetableCube : "1 Vegetable Cube",
		parsley : "1 Pack of Parsley" 
	}
}

//Functions

/**
 * Chops an igredient 
 * @param ingedient 
 * @return Chopped ingredient
 */
 chop(ingredient){
	 return ingredient / 15;
 }
 
 /**
 * Blends igredients 
 * @param {...*} ingredients
 * @return Blended ingredients
 */
 blend(...ingredients){
	return ingredients / 1000;
 }
 
 /**
 * Cooks an igredient 
 * @param ingedient 
 * @return Cooked ingredient
 */
 cook(ingredient){
	 return Cooked Ingredient;
 }

 /**
 * Mixes igredients
 * @param {...*} ingredients
 * @return Mixed Ingredients
 */
 mix(...ingredients){
	return Mixed Ingredients;
 }
 
 /**
 * Bakes igredients
 * @param {...*} ingredients
 * @param {Number} temperature (in celsius)
 * @param {Number} time (in minutes)
 * @return Baked Ingredients
 */
 bake(...igredients, time, temperature){
	 return Baked Ingredients;
 }
 
 /**
 * Serve the dish
 * @param readyDish
 * @param decoration
 * @param {Number} servings;
 * @return Serving in a plate
 */
 serve(readyDish, decoration, servings){
	 return (readyDish + decoration) / servings
 }

// [Step 1] - Chop the vegetalbes. 
var choppedOnions = chop(ingredients.main.onions);
var choppedPotatoes = chop(ingredients.main.potatoes);
var choppedCarrots = chop(ingredients.main.carrots);

// [Step 2] - Make the sauce
var sauce = blend(ingredients.main.cannedTomatoes, ingredients.main.water, ingredients.spices.oliveOil,
ingredients.spices.blackPepper, ingredients.spices.salt, ingredients.spices.parsley, 
ingredients.spices.vegetableCube);

// [Step 3] - Prepare the mince and onions mix
var cookedOnions = cook(choppedOnions);
var minceMix = fry(ingredients.main.porkMince, ingredients.main.beefMince, ingredients.spices.oil,
ingredients.spices.cummin, ingredients.spices.salt, ingredients.spices.blackPepper, 
ingredients.spices.paprika, ingredients.spices.grillSpices);

var friedMince = minceMix + cookedOnions;

// [Step 4] - Prepare the overlay
var overlay = mix(ingredients.overlay.eggs, ingredients.overlay.greekYogurt, ingredients.overlay.flour);

// [Step 5] - Mix everything together and bake
var mixedVegies = mix(choppedCarrots, choppedPotatoes);
var initialStageMusaka = bake(friedMince, mixedVegies, sauce, 60, 220);
var finalStageMusaka = bake(initialStageMusaka, overlay, 30, 220);

// [Step 6] - Serve the musaka
serve(finalStageMusaka, parsley, 4);

So here we go, that is a full recipe for Moussaka! If you are a programmer, I encourage you to try this recipe and start looking at cooking as something as fun as programming. On the other side, if you are a cook, trying this recipe and starting to put some programming thought in the process of cooking will definitely bring you benefits.

window.alert("Bon Appetit");

Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *