dylanlott
8/18/2017 - 6:25 PM

teem_interview_solutions.js

// `npm install axios lodash && node teem_interview_solutions.js`

const axios = require('axios')
const _ = require('lodash')

const collections = {
  '1': [10, 25, 13],
  '2': [14, 23, 47],
  '3': [14, 36, 23]
} 

const findMax = (collection, key) => {
  return Math.max.apply(null, collection[key]) 
}

console.log('##### findMax')
console.log(findMax(collections, '3'))

/*
  Coding Problem #2
  Write a method, `findRecord`, that will take an object collection, `records`
  and an integer, `object_id`.
  The method should iterate over `records` and return an object if the `object_id`
  matches the id of a record in the collection and throw an error message if the
  record id was not found
*/

const recordCollection = {
  1: {
    name: "Led Zeppelin",
    album: "Physical Graffiti",
    year: 1975
  },
  2: {
    name: "The Rolling Stones",
    album: "Exile on Main St.",
    year: 1972
  },
  3: {
    name: "Pink Floyd",
    album: "The Wall",
    year: 1979
  },
  4: {
    name: "Bob Dylan",
    album: "Blood on the Tracks",
    year: 1975
  },
  5: {
    name: "Marvin Gaye",
    album: "Whats Goin On",
    year: 1971
  }
};


const findRecord = (records, object_id) => {
  return records[object_id] ? records[object_id] : 'No record exists' 
}

console.log('##### findRecord')
console.log(findRecord(recordCollection, 2))

/*
  Coding Problem #3
  Write a method, `searchRecords`, that will take an object, `node`, a string, `key`,
  and string, `term`.
  The method should travers the tree to find all related nodes where `term` matches
  the `node`'s property for the given `key`.
  The original `node` should be considered immutable
  Additional Info:
  - `node` is expected to be a "node tree"
  - The `node` will have an imbedded object collection, `children`, that is a collection
    of nodes

  Example "node tree" object:
  {
    id: 1,
    name: 'Menu',
    description: 'Our super awesome food menu',
    children: []
  }
  Example of Search Results:
  term: 'Fr'
  key:name 
  Results: (simplified)
  - Menu
  |-- Combo #1
  | |-- French Fries
  |-- Combo #2
  | |-- Frozen Dairy Product
  |-- French Fries
  |-- Fresh Fruit
  |-- Frozen Dairy Product
*/

const nodeTree = {
  id: 1,
  name: 'Menu',
  description: 'Our Super Awesome Food',
  children: [
    {
      id: 1,
      name: 'Banana',
      description: 'Delicious Ripe Banana'
    },
    {
      id: 2,
      name: 'Whip Cream',
      description: 'Fluffy White Whip Cream'
    },
    {
      id: 3,
      name: 'Vanilla Icecream',
      description: 'Icecream mane from vanilla'
    },
    {
      id: 4,
      name: 'Cone',
      description: 'Cone'
    },
    {
      id: 5,
      name: 'Banana Split',
      description: 'Banana with whip cream and icecream',
      children: [
        {
          id: 1,
          name: 'Banana',
          description: 'Delicious Ripe Banana'
        },
        {
          id: 2,
          name: 'Whip Cream',
          description: 'Fluffy White Whip Cream'
        },
        {
          id: 3,
          name: 'Vanilla Icecream',
          description: 'Icecream mane from vanilla'
        }
      ]
    },
    {
      id: 6,
      name: 'Vanilla Icecream Cone',
      description: 'Delicious Vanilla Icecream Cone',
      children: [
        {
          id: 3,
          name: 'Vanilla Icecream',
          description: 'Icecream made from vanilla'
        },
        {
          id: 4,
          name: 'Cone',
          description: 'Cone'
        }

      ]
    }
  ]
}; 

const results = [] 
const loop = (node, key, term) => {
  if (node[key].indexOf(term) > -1) {
    results.push(node)
  }
  if (_.has(node, 'children')) {
    node.children.forEach(v => {
      loop(v, key, term)
    })
  } 
}

console.log('##### searchRecords')
loop(nodeTree, 'name', 'Banana')
console.log(results)

/*
  Coding Problem #4
  Write a method, `getRecords`, that will take a string, `recordType`, and an optional
  integer, `id`
  The method should query an API and return the result set for `recordType`
  If an `id` is provided, the record should be returned instead of the record set.
  Requirements:
  - Must use Promises or `async/await`
  - Must use JSONPlaceholder as your API, https://jsonplaceholder.typicode.com/.
*/

const BASE_URL = 'https://jsonplaceholder.typicode.com'
const getRecords = (recordType, id = '') => {
  console.log(`Request: ${BASE_URL}/${recordType}/${id}`)
  return axios.get(`${BASE_URL}/${recordType}/${id}`)
    .then((data) => console.log('data', data.data))
    .catch((err) => console.log('Error fetching data: ', err.message))
}

console.log('##### getRecords')
getRecords('posts')
getRecords('posts', '12')

/*
  Challenge Question
  Using the IMBD API create an API and UI in your favorite language(s) that would allow
  one to search movies, directors, actors, and IMBD votes.
  Notes:
  - Use http://theimdbapi.org/ or your favorite API for IMDB
  Requirements:
  The UI MUST:
  - be searchable by Title, Director, Actor, IMDB id,
  - pagination
  - detail view for Movie, Actor, Director.
  - sortable by Title, Director, Actor, IMBD likes.
*/

/*
  Bonus Question, Seven Degrees of Kevin Bacon...
  Using the IMDB

  Include a Page that would allow for user it select an actor and see their "degrees"
  from Kevin Bacon. You should allow for an optional "stop" on the lookup.
  (ie, 0 = Search until found, 3 = Stop looking after dept of 3 degrees)
*/