Skip to content

Instantly share code, notes, and snippets.

@rynbyjn
Last active October 24, 2019 20:15
Show Gist options
  • Save rynbyjn/b5848698653b4aa4be035a6a46620d40 to your computer and use it in GitHub Desktop.
Save rynbyjn/b5848698653b4aa4be035a6a46620d40 to your computer and use it in GitHub Desktop.
Code exercise with jurassic park actor filtering
// Data Sets:
// There are two data sets below: An object of humans with names as keys,
// and an array of Jurassic Park films.
// Goal: Create an array of objects that contain ONLY the humans who are
// NOT cast in any jurassic park films. The array of objects should have
// this structure:
// [
// {
// name: 'Tom Wilhoit',
// nationality: 'Kiwi',
// imdbStarMeterRating: 1
// }
// ]
export 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,
},
}
export 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',
],
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,
},
]
const jpActors = movies.reduce(
(acc, current) =>
acc.concat(current.cast.filter((actor: string) => acc.indexOf(actor) < 0)),
[],
)
const nonJpActors = (Object.keys(humans) as Array<keyof typeof humans>).reduce(
(acc, current) => {
if (jpActors.indexOf(current) < 0) {
const human = humans[current]
delete human.yearBorn
acc.push({ ...human, name: current })
}
return acc
},
[] as { imdbStarMeterRating: number; name: string; nationality: string }[],
)
console.log('jpActors', jpActors, nonJpActors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment