Daily Coding Challenge: July 14

Solved the daily coding challenge for July 14th:

const yearMap = {
  dog: 7,
  cat: 6,
  rabbit: 8,
  hamster: 30,
 "guinea pig": 12,
  goldfish: 6,
  bird: 5
}

function petYears(pet, age) {
  return yearMap[pet] * age;
}

Pretty simple. Anyone else solve it with a different solution?

Elegant. Nicely done.

Here’s mine:

function petYears(pet, age) {
  const petAgeCalculator = [
    { pet: "dog", multiplier: 7 },
    { pet: "cat", multiplier: 6 },
    { pet: "rabbit", multiplier: 8 },
    { pet: "hamster", multiplier: 30 },
    { pet: "guinea pig", multiplier: 12 },
    { pet: "goldfish", multiplier: 6 },
    { pet: "bird", multiplier: 5 }
  ];
  const petObj =  petAgeCalculator.find(el => el.pet === pet);
  return petObj.multiplier * age;
}

Update:
I use an array of objects with find() a lot in my code, but your solution has me going back through my code to see where I can replace an array of objects with a simple object, which is more performant. I love being able to learn better ways of coding! I also learned that a Map is a great performer for lookups, especially with large datasets.

@moT01

Solved? I thought you wrote them.

Do you have a process for writing them?
Are they going to sunset?

Happy coding