Breaking Down Pony Farm Toy Problem

Hello readers, in this article I will explain the importance of solving toy problems while on the journey to becoming a Software Engineer, these benefits will also be useful for maintaining your skills while working through your career.

This article was written for my fellow coding bootcampers going through the same experience more or less than I am just finishing up. Almost everyday we are given a toy problem to work on to start off our day. This is really beneficial because as I’ve heard throughout my life and not just in coding boot camp ‘It’ll get the juices flowing.” Not only does it get your brain working but your mind becomes more alert and aware. Additionally it sharpens your ability to problem solve, and learn a-lot about the cause and effect that code can have. Completing these problems also boosts ones self esteem which can lead to be even more of a productive day because you are in a positive mind set.

The other day I was given a toy problem with the title “Pony Farm”. The story was as follows….”A certain pony farm (cage-free) raises and keeps track of cowboys’ and cowgirls’ ponies. Write a function, getPonyAllergies, that takes in an array of pony objects and an owner's email address and returns an alphabetically ordered array of all the foods--without duplicates--that the owner should avoid feeding their ponies.”

So let’s break down this problem so we can better understand what the objective is: A certain pony farm(cage-free) raises and keeps track of cowboys and cowgirls ponies…. Ok so this bit doesn’t really give a whole lot but the take away is that we are probably going to deal with male and female horse owners. “ A certain pony farm(cage-free) raises and keeps track of cowboy’s and cowgirl’s ponies.” Ok so now in this section it confirms out assumption that we are going to be dealing with records of pony owners. “Write a function, getPonyAllergies, that takes in an array of pony objects and an owner’s email address and returns an alphabetically ordered array of all the foods — without duplicates — that the owner should avoid feeding their ponies.” Ok this section is our meat and potatoes, if you will. So the first thing that is mentioned is that we need to write a function, and it even gives us the name “getPonyAllergies”. So let’s add that to the list of things to do, which should be the first thing. Next we have “that takes in an array of pony objects and an owner’s email address and returns an alphabetically ordered array of all the foods”statement which we can break down into segments. The first letting us know that the function takes in an array of objects and an owners email address. So the takeaway from this segment is… Ok we now know what we are given, a list of ponies and a person’s email address thats going to be on that list. The second segment, what we can take from it is that it needs to return an alphabetically ordered array of all the foods. Then we have “ — without duplicates — “ which lets us know ok this list needs to be unique. Lastly “that the owner should avoid feeding their ponies”. This should have been assumed when seeing the function name. So now that we have broken down the story for the toy problem lets solve this bad boy!

const getPonyAllergies = (ponies, ownerEmail) => {}

Ok step one of the checklist done, created a function that takes in a pony list and an owners email.

const getPonyAllergies = (ponies, ownerEmail) => {let dontFeedList = [];// return an alphabetically ordered array of all foods without duplicates to avoidontFeedListreturn dontFeedList;}

Step 2 complete, return a list of unique foods to not feed the pony. So we know that if we are returning a list we will need to first create one.

Now that we have somewhat of a map of beginning to end, we now must make the journey or in this case actually implement how this is going to happen. There are many different ways to go about this some shorter or longer than others but this is how my mind tackled this problem. We are given a list of ponies which are themselves objects and an email. Now we must go through this list and get the information needed or requested. You could loop through this but I chose to create a new variable and set it equal to a filtered version of that array but using the higher order function filter. Using the filter function requires that you pass another function in to test the current value and it returns a boolean. This boolean will be the result of comparing the current pony and the email provided. So if the the pony has the given email address we are going to keep that and forget about the rest. It looks a little something like this now.

let dontFeedList = [];//create a list of only the ponys  that contain the ownerEmaillet filtered = ponies.filter(pony => pony.email === ownerEmail ? true : false);// return an alphabetically ordered array of all foods without duplicates to avoidontFeedListreturn dontFeedList;}

So now that we have a list of only the ponies that matter we can go through this list and extract the items that we need to not feed them. I accomplished this by mapping over the filtered array as shown below.

let dontFeedList = [];//create a list of only the ponys  that contain the ownerEmaillet filtered = ponies.filter(pony => pony.email === ownerEmail ? true : false);//go through the new list and extact the items to avoid and put them in our return arrayfiltered.map(ponie => results.push(ponie.allergies));// return an alphabetically ordered array of all foods without duplicates to avoidontFeedListreturn dontFeedList;}

Alright we have now a list of all the items to not feed our Pony! Unfortunately we have to now account for our restrictions made, which were to have a unique list which means no duplicate items. To ensure that this list only has items appear once we can do this several different ways but the quickest way or the way done with what I thought was the least amount of code was to do it like this….

let uniqueResults = [...new Set(results.flat(Infinity).sort())];

So we finally have a list of items we are not going to feed our ponies. Now we must do one last thing which is change the name of the result array we are returning. If you remember when we first set up this function we gave it a return value of the original array we pushed into with the filtered ponies.

So to put it all together….

Thank you all for reading, I hope you enjoyed this COMPLETE breakdown of a very fun toy problem to solve.