Skip to content

Instantly share code, notes, and snippets.

@trishalanglois
Last active January 2, 2020 01:26
Show Gist options
  • Save trishalanglois/0e82da8893d494d82d6829b8b71e58f0 to your computer and use it in GitHub Desktop.
Save trishalanglois/0e82da8893d494d82d6829b8b71e58f0 to your computer and use it in GitHub Desktop.

The Challenge

Given the following data sets, return an array of objects for each human and the age(s) they were in the movie(s) they were cast in, as an array of age(s). Only include humans who were cast in at least one movie.

const dinosaurs = { 
  Brachiosaurus: { carnivore: false, herbivore: true, isAwesome: true },
  Dilophosaurus: { carnivore: true, herbivore: false, isAwesome: true },
  Gallimimus: { carnivore: true, herbivore: true, isAwesome: false },
  Triceratops: { carnivore: false, herbivore: true, isAwesome: true },
  Parasaurolophus: { carnivore: false, herbivore: true, isAwesome: false },
  'Tyrannosaurus Rex': { carnivore: true, herbivore: false, isAwesome: true },
  Velociraptor: { carnivore: true, herbivore: false, isAwesome: true },
  Compsognathus: { carnivore: true, herbivore: false, isAwesome: true },
  Mamenchisaurus: { carnivore: false, herbivore: true, isAwesome: true },
  Pachycephalosaurus: { carnivore: false, herbivore: true, isAwesome: true },
  Pteranodon: { carnivore: true, herbivore: false, isAwesome: true },
  Stegosaurus: { carnivore: false, herbivore: true, isAwesome: true },
  Ankylosaurus: { carnivore: false, herbivore: true, isAwesome: true },
  Ceratosaurus: { carnivore: true, herbivore: false, isAwesome: true },
  Corythosaurus: { carnivore: false, herbivore: true, isAwesome: false },
  Spinosaurus: { carnivore: true, herbivore: false, isAwesome: true },
  Apatosaurus: { carnivore: false, herbivore: true, isAwesome: true },
  Dimorphodon: { carnivore: true, herbivore: false, isAwesome: true },
  'Indominus Rex': { carnivore: true, herbivore: false, isAwesome: true },
  Mosasaurus: { carnivore: true, herbivore: false, isAwesome: true },
  Allosaurus: { carnivore: true, herbivore: false, isAwesome: true },
  Baryonyx: { carnivore: true, herbivore: false, isAwesome: true },
  Carnotaurus: { carnivore: true, herbivore: false, isAwesome: true },
  Indoraptor: { carnivore: true, herbivore: false, isAwesome: true },
  Sinoceratops: { carnivore: true, herbivore: true, isAwesome: true },
  Stygimoloch: { carnivore: false, herbivore: true, isAwesome: true } 
};

const humans = {
  'Sam Neill': {
    yearBorn: 1947,
    nationality:'Irish',
    imdbStarMeterRating: 5000
  },
  'Tom Wilhoit':{
    yearBorn: 1777,
    nationality:'Kiwi',
    imdbStarMeterRating:1
  },
  'Laura Dern': {
    yearBorn: 1967,
    nationality:'American',
    imdbStarMeterRating: 5000
  },
  'Jeff Goldblum': {
    yearBorn: 1952,
    nationality:'American',
    imdbStarMeterRating:5000
  },
  'Jeo D': {
    yearBorn: 1776,
    nationality:'Martian',
    imdbStarMeterRating:0
  },
  'Richard Attenborough': {
    yearBorn: 1923,
    nationality:'British',
    imdbStarMeterRating:5000
  },
  'Ariana Richards': {
    yearBorn: 1979,
    nationality:'American',
    imdbStarMeterRating:5000
  },
  'Joseph Mazello': {
    yearBorn: 1983,
    nationality:'American',
    imdbStarMeterRating: 21
  },
  'Justin Duncan': {
    yearBorn: 1775,
    nationality:'Alien',
    imdbStarMeterRating: 0
  },
  'BD Wong': {
    yearBorn: 1960,
    nationality:'American',
    imdbStarMeterRating:5000
  },
  'Chris Pratt': {
    yearBorn: 1979,
    nationality:'American',
    imdbStarMeterRating:500
  },
  'Karin Ohman': {
    yearBorn: 1995,
    nationality: 'Chinese',
    imdbStarMeterRating: 0
  },
  'Bryce Dallas Howard': {
    yearBorn: 1981,
    nationality:'American',
    imdbStarMeterRating:80
  }
}; 

const movies = [
  {
    title: 'Jurassic Park',
    director: 'Steven Spielberg',
    leadingActor: 'Sam Neill',
    cast: [
      'Sam Neill',
      'Laura Dern',
      'Jeff Goldblum',
      'Richard Attenborough',
      'Ariana Richards',
      'Joseph Mazello',
      'BD Wong'
    ],
    dinos: [
      'Brachiosaurus',
      'Dilophosaurus',
      'Gallimimus',
      'Triceratops',
      'Parasaurolophus',
      'Tyrannosaurus Rex',
      'Velociraptor'
    ],
    yearReleased: 1993,
    hasOscar: true,
    millionsGrossed: 1029,
  },
  {
    title: 'The Lost World: Jurassic Park',
    director: 'Steven Spielberg',
    leadingActor: 'Jeff Goldblum',
    cast: [
      'Jeff Goldblum',
      'Richard Attenborough',
      'Ariana Richards',
      'Joseph Mazello'
    ],
    dinos: [
      'Compsognathus',
      'Gallimimus',
      'Mamenchisaurus',
      'Pachycephalosaurus',
      'Parasaurolophus',
      'Pteranodon',
      'Stegosaurus',
      'Triceratops',
      'Tyrannosaurus Rex',
      'Velociraptor'
    ],
    yearReleased: 1997,
    hasOscar: false,
    millionsGrossed: 619
  },
  {
    title: 'Jurassic Park III',
    director: 'Joe Johnston',
    leadingActor: 'Sam Neill',
    cast: ['Sam Neill', 'Laura Dern'],
    dinos: ['Ankylosaurus', 'Brachiosaurus', 'Ceratosaurus', 'Corythosaurus', 'Parasaurolophus', 'Pteranodon', 'Spinosaurus', 'Stegosaurus', 'Triceratops', 'Tyrannosaurus Rex', 'Velociraptor'],
    cast: [
      'Sam Neill',
      'Laura Dern'
    ],
    dinos: [
      'Ankylosaurus',
      'Brachiosaurus',
      'Ceratosaurus',
      'Corythosaurus',
      'Parasaurolophus',
      'Pteranodon',
      'Spinosaurus',
      'Stegosaurus',
      'Triceratops',
      'Tyrannosaurus Rex',
      'Velociraptor'
    ],
    yearReleased: 2001,
    hasOscar: false,
    millionsGrossed: 369
  },
  {
    title: 'Jurassic World',
    director: 'Colin Trevorrow',
    leadingActor: 'Chris Pratt',
    cast: [
      'Jeff Goldblum',
      'Richard Attenborough',
      'BD Wong',
      'Chris Pratt',
      'Bryce Dallas Howard'
    ],
    dinos: [
      'Ankylosaurus',
      'Apatosaurus',
      'Dimorphodon',
      'Gallimimus',
      'Indominus Rex',
      'Mosasaurus',
      'Pachycephalosaurus',
      'Parasaurolophus',
      'Pteranodon',
      'Stegosaurus',
      'Triceratops',
      'Tyrannosaurus Rex',
      'Velociraptor'
    ],
    yearReleased: 2015,
    hasOscar: false,
    millionsGrossed: 1672
  },
  {
    title: 'Jurassic World: Fallen Kingdom',
    director: 'J. A. Bayona',
    leadingActor: 'Chris Pratt',
    cast: [
      'Jeff Goldblum',
      'Richard Attenborough',
      'BD Wong',
      'Chris Pratt',
      'Bryce Dallas Howard'
    ],
    dinos: [
      'Allosaurus',
      'Ankylosaurus',
      'Apatosaurus',
      'Baryonyx',
      'Brachiosaurus',
      'Carnotaurus',
      'Compsognathus',
      'Dilophosaurus',
      'Gallimimus',
      'Indominus Rex',
      'Indoraptor',
      'Mosasaurus',
      'Parasaurolophus',
      'Pteranodon',
      'Sinoceratops',
      'Stegosaurus',
      'Stygimoloch',
      'Triceratops',
      'Tyrannosaurus Rex',
      'Velociraptor'
    ],
    yearReleased: 2018,
    hasOscar: false,
    millionsGrossed: 1304
  }
];

The Process

Any time I'm working with array prototypes, I start with pseudocoding. I break my psuedocoding into steps, which are commented out.

  1. What is the input? - in this case, the movies array
  2. What is the output? - an array of objects that isn't a subset of the original array that I'm putting in
  3. The answer to #1 and #2 will likely guide me in what overall prototype to use - in this case, .reduce()
  4. From there, I go through and list the modular steps in as much detail as possible. For this example, my pseudocoding looked like this:

input array of movies -- .reduce()

iterate through cast of each movie

for each actor, find the actor's age

in the accumulator, if the actor object does not exist, create it

after object has been created, push age into ages array in the overall accumulator

output accumulator -- array of objects

From there, I start writing code.

Reflections as I solved this:

  • Naming conventions: The hardest thing about coding is naming things, right? Because there were so many moving pieces that had to do with actors, I named the overall accumulator something quirky and was very specific with accumulatorActor. Normally, I try to stay away from naming things after their data type or "accumulator", but I made an exception for this problem.
  • The actual placement of pushing the ages into the accumulator is most cleanly done outside of creating the object literal. At first, I was trying to do it all at once, but I found that this wasn't the best to do in the long-run, anyway, since I needed to add the age even when the actor object had already been created.
  • Using .some() was a first for me. Instead of using .find(), I simply wanted to check and see if the actor object had been created yet, so this was an ideal method to do that that returned a simple boolean.
  • Using a combination of bracket and dot notation to access when the actor was born was an exciting example of when it's useful to use both types of notation at the same time.

The Solution

function actorsAgesInMovies() {
  return movies.reduce((bigDaddyAcc, movie) => {
    movie.cast.forEach(actor => {
      let age = movie.yearReleased - humans[actor].yearBorn;
      if (!bigDaddyAcc.some(person => person.name === actor)) {
        bigDaddyAcc.push({name: actor, ages: []})
      }
      bigDaddyAcc.find(accumulatorActor => accumulatorActor.name === actor).ages.push(age)
    })
    return bigDaddyAcc
  }, []);
};

actorsAgesInMovies();

The Output

[ { name: 'Sam Neill', ages: [ 46, 54 ] },
  { name: 'Laura Dern', ages: [ 26, 34 ] },
  { name: 'Jeff Goldblum', ages: [ 41, 45, 63, 66 ] },
  { name: 'Richard Attenborough', ages: [ 70, 74, 92, 95 ] },
  { name: 'Ariana Richards', ages: [ 14, 18 ] },
  { name: 'Joseph Mazello', ages: [ 10, 14 ] },
  { name: 'BD Wong', ages: [ 33, 55, 58 ] },
  { name: 'Chris Pratt', ages: [ 36, 39 ] },
  { name: 'Bryce Dallas Howard', ages: [ 34, 37 ] } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment