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 result = _.pluck((_.filter(data,function(n){return _.contains(n.name,"J")})),"name");
return result

How many Johns?

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

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 result = _.map(data,function(n){return _.first(n.name.split(" "))})
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 result = 'not done'
var fullname= _.pluck(_.filter(data,function(n){return _.contains(n.name,"Smith")}),"name")
return _.map(fullname,function(n){return _.first(n.split(" "))})

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(n){var split =_.words(n.name)
return {name : _.last(split).concat(", ").concat(_.first(split))}
})
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 result = _.size(_.filter(data,'gender','f'))
return result

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 fullname =_.filter(data,'gender','m')
var people =_.filter(fullname,function(n){return _.contains(n.name ,"Smith")})
return _.size(people);

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 male =_.size(_.filter(data,'gender','m'))
var female =_.size(_.filter(data,'gender','f'))
if(male>female)
{
return true
}
else{
return false}

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 =_.find(data,function(n){return n.name=='Peter Pan'}).gender
return result

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 result = _.last(_.pluck(_.sortBy(data,"age"),'age'))
return result

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 result = _.all(data,function(n){ return n.age<60})
return result

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 result = _.some(data,function(n){ return n.age<18})
return result

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 = _.size(_.filter(data,function(n){return _.any(n.favorites,function(t){return t=='food'})}))
return 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 result =_.pluck(_.filter(data,function(n){return (n.age>40 )&&  _.any(n.favorites,function(t){return t=='travel'})}),"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 foodypeople=_.filter(data,function(n){return _.any(n.favorites,function(t){return t=='food'})})
var result = _.last(_.pluck(_.sortBy(foodypeople,'age'),'name'))
return result

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 result = _.uniq(_.flatten(_.pluck(data,"favorites")))
return result

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 lastname = _.map(data,function(n){return _.last(n.name.split(" "))})

var result = _.uniq(lastname)
return result