DEV Community

Cover image for Pony Farm
NaseerHines
NaseerHines

Posted on

Pony Farm

Attention coders, readers, and other enthusiasts I've returned for yet another blog on my latest code challenge. So I thought I would make today's entry a little interesting by talking about, you guessed it ponies. Yep, that's right we together shall embark on a journey of ponies and what their allergies are. Well, I'm no veterinarian so I don't know if these are legit but as for this post, they are so yeah.

A certain pony farm 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 before we can jump into my solution as always we have more information to think about when we are writing our code.
Giving ourselves some challenges we can take into account when writing this code:
-You must order your returned allergies alphabetically.
-You must use Array.prototype.map or Array.prototype.flatMap.
-You must use Array.prototype.filter.
-You must not return any duplicate allergies.

My solution

const getPonyAllergies = (ponies, ownerEmail) => {
  const isEmail = obj => obj.email === ownerEmail;

  const allergies = obj => obj.allergies;

  const concat = (i, curr) => i + ',' + curr;

  const uniq = (item, pos, ary) => !pos || item !== ary[pos - 1];

  const str = ponies.filter(isEmail).map(allergies).reduce(concat);

  if (Array.isArray(str)) {
    return str.sort().filter(uniq);
  } else {
    return str.split(",").sort().filter(uniq);
  }
};
  1. So first we make a callback function to check if the current object's email is the same as the passed in one.
  2. Then we make a function to give us only the objects allergies array.
  3. The concat function is just making sure we correctly add them to a new return array. uniq function is making sure we don't have any duplicates.

now we can make our array of allergies with

const str = ponies.filter(isEmail).map(allergies).reduce(concat);

Now we can just check if the string is an array if it is we sort then filter according to our uniq function. If it's not then we make it an array and then follow the back half of the first condition.

Let's make an array of test data(ponies in our pony farm)
we need them to have a few things

  • an id.
  • a name.
  • known allergies.
  • owners email.
const ponies = [
  {
    id: 427,
    name: 'Rarity',
    allergies: ['gluten', 'peanut'],
    email: 'cindy@ponymail.com',
  },
  {
    id: 23,
    name: 'Pinkie Pie',
    allergies: ['soy', 'peanut'],
    email: 'sandy@hotmail.com',
  },
  {
    id: 458,
    name: 'Twilight Sparkle',
    allergies: ['corn', 'gluten'],
    email: 'cindy@ponymail.com',
  },
  {
    id: 142,
    name: 'Rainbow Dash',
    allergies: ['gluten', 'chicken'],
    email: 'sandy@hotmail.com',
  },
  {
    id: 184,
    name: 'Applejack',
    allergies: ['soy', 'peanut', 'gluten'],
    email: 'jimmy@ponymail.com',
  },
];

If we test the code out with the following

const ownerEmail = 'sandy@hotmail.com';
getPonyAllergies(ponies, ownerEmail); 

getPonyAllergies should return

['chicken', 'gluten', 'peanut', 'soy']

Ya so that was my last toy problem wasn't so bad, to be honest. I like these sort of problems because it just seems more fun overall. It doesn't just leave you to dry code which I don't think most people like. Welp, see you homies in the next one happy coding.

Top comments (0)