book

Lodash Drills

Complete the following Lodash exercises. The goal is to become really good at functional programming paradigm (e.g., _.map, _.filter, _.all, _.any ...etc) and a number of really useful Lodash methods (e.g., _.find, _.pluck ... etc).

  • enter your solution in the solution block
  • utilize lodash functions as much as possible
  • no for/while loop is allowed

Familiarity with programming in this way will not only make you a super productive programmer but also will pave the way for you to learn MapReduce and MongoDB.

Examples

How many people?

Done
Data
[{name: 'John'}, {name: 'Mary'}, {name: 'Joe'}, {name: 'Ben'}]
Expected Output
4
Actual Output
4
Solution
return data.length

What are the names?

Done
Data
[{name: 'John'}, {name: 'Mary'}, {name: 'Joe'}, {name: 'Ben'}]
Expected Output
[
  "John",
  "Mary",
  "Joe",
  "Ben"
]
Actual Output
[
  "John",
  "Mary",
  "Joe",
  "Ben"
]
Solution
return _.map(data, function(d){
    return d.name
})

Exercises

What names begin with the letter J?

Done
Data
[{name: 'John'}, {name: 'Mary'}, {name: 'Joe'}, {name: 'Ben'}]
Expected Output
[
  "John",
  "Joe"
]
Actual Output
[
  "John",
  "Joe"
]
Solution
var names =  _.pluck(data, 'name')
var result = _.filter(names, function(d) {
  if (d[0] == "J") return d
});
return result

How many Johns?

Done
Data
[{name: 'John'}, {name: 'John'}, {name: 'John'}, {name: 'Ben'}]
Expected Output
3
Actual Output
3
Solution
var result = _.pluck(_.filter(data, {name: 'John'}), 'name');
return result.length

What are all the first names?

Done
Data
[{name: 'John Smith'}, {name: 'Mary Kay'}, {name: 'Peter Pan'}, {name: 'Ben Franklin'}]
Expected Output
[
  "John",
  "Mary",
  "Peter",
  "Ben"
]
Actual Output
[
  "John",
  "Mary",
  "Peter",
  "Ben"
]
Solution
var names =  _.pluck(data, 'name')
var result = _.map(names, function(d) {
  var firsts = d.split(' ')[0]
  return firsts
});
return result

What are the first names of Smith?

Done
Data
[{name: 'John Smith'}, {name: 'Mary Smith'}, {name: 'Peter Pan'}, {name: 'Ben Smith'}]
Expected Output
[
  "John",
  "Mary",
  "Ben"
]
Actual Output
[
  "John",
  "Mary",
  "Ben"
]
Solution
var people =  _.pluck(data, 'name')
var names = _.map(people, function(n) {
  return n.split(' ')
});
var smiths = _.flatten(_.filter(names, function(n) {
  if (n[1] == "Smith") return n[0]
}));
var result = _.remove(smiths, function(n) {
  return n != "Smith";
});
return result

Change the format to lastname, firstname

Done
Data
[{name: 'John Smith'}, {name: 'Mary Kay'}, {name: 'Peter Pan'}]
Expected Output
[
  {
    "name": "Smith, John"
  },
  {
    "name": "Kay, Mary"
  },
  {
    "name": "Pan, Peter"
  }
]
Actual Output
[
  {
    "name": "Smith, John"
  },
  {
    "name": "Kay, Mary"
  },
  {
    "name": "Pan, Peter"
  }
]
Solution
var names =  _.pluck(data, 'name')
var lastFirst = _.map(names, function(d) {
  var first = d.split(' ')[0];
  var last = d.split(' ')[1];
  return {"name": last.toString() + ", " + first.toString()}
});

return lastFirst

How many women?

Done
Data
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
Expected Output
1
Actual Output
1
Solution
var result = _.pluck(_.filter(data, {gender: 'f'}), 'gender');
return result.length

How many men whose last name is Smith?

Done
Data
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
Expected Output
2
Actual Output
2
Solution
var men = _.pluck(_.filter(data, {gender: 'm'}), 'name');
var names = _.map(men, function(n) {
  return n.split(' ')
});
var smiths = _.flatten(_.filter(names, function(n) {
  if (n[1] == "Smith") return n[0]
}));
var result = _.remove(smiths, function(n) {
  return n == "Smith";
});
return result.length

Are there more men than women?

Done
Data
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
Expected Output
true
Actual Output
true
Solution
var women = _.pluck(_.filter(data, {gender: 'f'}), 'gender');
var men = _.pluck(_.filter(data, {gender: 'm'}), 'gender');
return men.length > women.length

What is Peter Pan's gender?

Done
Data
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
Expected Output
"m"
Actual Output
"m"
Solution
var result = _.pluck(_.filter(data, {name: 'Peter Pan'}), 'gender');
return result[0]

What is the oldest age?

Done
Data
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Ben Smith', age: 35}]
Expected Output
54
Actual Output
54
Solution
return _.max(_.pluck(data, 'age'))

Is it true everyone is younger than 60?

Done
Data
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Ben Smith', age: 35}]
Expected Output
true
Actual Output
true
Solution
return _.all(data, function(d) {
  return d.age < 60
});

Is it true someone is not an adult (younger than 18)?

Done
Data
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Ben Smith', age: 35}]
Expected Output
true
Actual Output
true
Solution
return _.some(data, function(d) {
  return d.age < 18
});

How many people whose favorites include food?

Done
Data
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']},
 {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']},
 {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']},
 {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
Expected Output
3
Actual Output
3
Solution
var favorites = _.pluck(data, 'favorites')
var food = _.countBy(favorites, function(n){
  return _.includes(n, "food")
})
return food.true

Who are over 40 and love travel?

Done
Data
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']},
 {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']},
 {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']},
 {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']},
 {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
Expected Output
[
  "Mary Smith",
  "Joe Johnson"
]
Actual Output
[
  "Mary Smith",
  "Joe Johnson"
]
Solution
var lovesTravel = _.map(data, function(d){
  if ((d.age > 40) && (_.includes(d.favorites, 'travel'))) return d.name
})
var names = _.filter(lovesTravel, function(d){
  if (d != null) return d
})

return names

Who is the oldest person loving food?

Done
Data
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']},
 {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']},
 {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']},
 {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']},
 {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
Expected Output
"John Smith"
Actual Output
"John Smith"
Solution
var lovesFood = _.map(data, function(d){
  if (_.includes(d.favorites, 'food')) return d
})

return (_.max(lovesFood, 'age')).name

What are all the unique favorites?

Done
Data
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']},
 {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']},
 {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']},
 {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']},
 {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
Expected Output
[
  "food",
  "movies",
  "travel",
  "minecraft",
  "pokemo",
  "craft"
]
Actual Output
[
  "food",
  "movies",
  "travel",
  "minecraft",
  "pokemo",
  "craft"
]
Solution
var favorites = _.pluck(data, 'favorites')
return _.uniq(_.flatten(favorites))

What are all the unique last names?

Done
Data
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']},
 {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']},
 {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']},
 {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']},
 {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
Expected Output
[
  "Smith",
  "Pan",
  "Johnson"
]
Actual Output
[
  "Smith",
  "Pan",
  "Johnson"
]
Solution
var names =  _.pluck(data, 'name')
var result = _.map(names, function(d) {
  var firsts = d.split(' ')[1]
  return firsts
});
return _.uniq(result)