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 list = _.filter(data, function(element){
  var name=element.name
  return name.charAt(0) == 'J'
})
var result=_.map(list, function(element){
  return element.name
})
return result

How many Johns?

Done
Data
[{name: 'John'}, {name: 'John'}, {name: 'John'}, {name: 'Ben'}]
Expected Output
3
Actual Output
3
Solution
var list = _.filter(data, function(element){
  var name=element.name
  return name == 'John'
})
return _.size(list)

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 list = _.map(data, function(element){
  var name=element.name.split(" ")
  return name[0]
})
return list

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 list = _.filter(data, function(element){
  var name=element.name.split(" ")
  return name[1] == 'Smith'
})
var result = _.map(list, function(element){
  var name=element.name.split(" ")
  return name[0]
})
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 result = _.map(data, function(element){
  var name=element.name.split(" ")
  return {"name": name[1]+", "+name[0]}
})
return result

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 list = _.filter(data, function(element){
  return element.gender == 'f'
})
return _.size(list)

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 list = _.filter(data, function(element){
  var lastName=element.name.split(" ")[1]
  return element.gender == 'm' && lastName == 'Smith'
})
return _.size(list)

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 listM = _.filter(data, function(element){
  return element.gender == 'm'
})
var listF = _.filter(data, function(element){
  return element.gender == 'f'
})
return _.size(listM) > _.size(listF)

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 list = _.filter(data, function(element){
  return element.name == 'Peter Pan'
})
return list[0].gender

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
var ages = _.map(data, function(element){
  return element.age
})
return _.max(ages)

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
// use _.all
var ages = _.filter(data, function(element){
  return element.age > 60
})
return _.size(ages) == 0

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
// use _.some
var ages = _.filter(data, function(element){
  return element.age < 18
})
return _.size(ages) > 0

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 result = _.filter(data, function(element){
  var food=_.filter(element.favorites, function(favorite){
    return favorite == 'food'
  })
  return _.size(food) > 0
})
return _.size(result)

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 list = _.filter(data, function(element){
  var travel=_.filter(element.favorites, function(favorite){
    return favorite == 'travel'
  })
  return _.size(travel) > 0 && element.age > 40
})
var result = _.map(list, function(element){
  return element.name
})
return result

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 list = _.filter(data, function(element){
  var food=_.filter(element.favorites, function(favorite){
    return favorite == 'food'
  })
  return _.size(food) > 0
})
var person = _.max(list, function(element){
  return element.age
})
return person.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
// hint: use _.pluck, _.uniq, _.flatten in some order

var favorites = _.pluck(data, 'favorites')
var favList=_.flatten(favorites)
var uniqList=_.uniq(favList)
return uniqList

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 list = _.map(data, function(element){
  return element.name.split(' ')[1]
})
return _.uniq(list)