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 _.startsWith(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");
}))
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(_.words(n.name));
})
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 S = _.filter(data,function(n){
  return _.last(_.words(n.name)) == 'Smith';
})
var result = _.map( S, function(n){
  return _.first(_.words(n.name));
})
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(n){
  return {"name": _.last(_.words(n.name))+ ", "+_.first(_.words(n.name))}
})
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,function(n){
  return n.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 S = _.filter(data,function(n){
  return n.gender == 'm';
})
var S2 = _.filter(S,function(n){
  return _.last(_.words(n.name)) == 'Smith';
})
var result = _.size(_.map(_.pluck(S2,'name'), function(n){
  return _.last(_.words(n));
}));
return result;

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 m = _.size(_.filter(data, function(n){
  return n.gender == 'm';
}));
var f = _.size(_.filter(data,function(n){
  return n.gender == 'f';
}));
if( m>f) return true;else 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 obj = _.find(data, 'name', "Peter Pan");
var result = obj.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 = _.max( _.pluck(data, '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
return _.some( data, {'age': _.min(_.pluck(data,'age'))});

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 _.includes(n.favorites, '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 (_.includes(n.favorites,'travel') && n.age > 40);
}),'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 S = _.filter(data,function(n){
  return _.includes(n.favorites,'food');
});
var obj = _.find(S, { 'age': _.max(_.pluck(data, 'age'))}, 'name');
var result = obj.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 S = _.pluck(data, 'favorites');
var result = _.uniq(_.flatten(S));
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 obj = _.map(_.pluck(data,'name'), function(n){
  return _.last(_.words(n));
});

var result = _.uniq(obj)

return result