Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 3, 2018 12:02
Show Gist options
  • Save Anna-Myzukina/8b50faf1cc02c2dc16d45b8aa68ebb97 to your computer and use it in GitHub Desktop.
Save Anna-Myzukina/8b50faf1cc02c2dc16d45b8aa68ebb97 to your computer and use it in GitHub Desktop.
Lolodash
//Exercise 5 [_.chain(value)]
/*We have a list of words that we want to modify.
['Test', 'Hello', 'World', 'Node', 'JavaScript']
We want to modify each word so that they are all appended with the word Chained, converted to uppercase, and sorted by alphabetical order. The result should look like this:
['HELLOCHAINED', 'JAVASCRIPTCHAINED', 'NODECHAINED', 'TESTCHAINED', 'WORLDCHAINED']
*/
var _ = require('lodash');
var worker = function(list){
return _.chain(list)
.sortBy(function(a){return a})
.map(function(a){return a.toUpperCase() + 'CHAINED'})
.value()
}
module.exports = worker;
//Exersise 1 [_.filter(collection, [predicate=_.identity])]
/*We have an array of Javascript Objects representing some users in the following format:
[
{ id: 22, username: "martin", active: true},
{ id: 23, username: "max", active: false},
{ id: 24, username: "linda", active: false}
]
Please write a function that takes such a list as its first argument and give back only
the active users (active === true).
*/
var _ = require('lodash');
var worker = function(collection){
return _.filter(collection, {active : true});
}
module.exports = worker;
//Exersise 1 [_.filter(collection, [predicate=_.identity])]
/*We have an array of Javascript Objects representing some users in the following format:
[
{ id: 22, username: "martin", active: true},
{ id: 23, username: "max", active: false},
{ id: 24, username: "linda", active: false}
]
Please write a function that takes such a list as its first argument and give back only
the active users (active === true).
*/
var _ = require('lodash');
var worker = function(collection){
return _.filter(collection, {active : true});
}
module.exports = worker;
//Exercise 6 [_.groupBy(collection, [callback=identity])]
/*We have an array of comments from the website:
[
{ username: "tim", comment: "you are doing a great job!" },
{ username: "tim", comment: "when you have new workshoppers?" },
{ username: "cat_lover", comment: "wtf? where are all the cats gone?" },
{ username: "max", comment: "where have you been on friday? we missed you!" },
{ username: "max", comment: "You don't answer anymore - why?" },
{ username: "cat_lover", comment: "MORE cats!!!" },
{ username: "max", comment: "i really love your site" }
]
I want to know how to post the most comments without repeating the same user,
so please write a function that:
* Counts the comments by `username`.
* Sorts the return array by the total comment count of each user.
[ { username: "foo", comment_count: 9 },
{ username: "foobar", comment_count: 2 } ]
*/
var _ = require('lodash');
var worker = function(list){
var counted = []
var grouped = _.groupBy(list, 'username');
_.forEach(grouped, function(value, key){
counted.push({'username' : key, 'comment_count': value.length})
});
return counted.reverse();
}
module.exports = worker;
//Exercise 3 [_.forEach(collection, [iteratee=_.identity])]
/*Please write a function that takes in a hashtable or associative array of
European towns with their populations in millions:
{ Hamburg: { population: 1.698 },
Strasbourg: { population: 0.272 },
Rome: { population: 2.753 },
Dublin: { population: 0.528 } }
We want a new size attribute added to every town where the value depends on
the town's population, as follows:
{ City1: {population: 1.58, size: 'big' },
City2: {population: 0.58, size: 'med' },
City3: {population: 0.28, size: 'small'} }
Attention: the list could also be an Array, so you will have to use a
collection function.
Use the following guidelines:
* All cities having a population over 1 million are "big".
* Cities having a population less than 1 million but more than 0.5 million are "med".
* All cities having a population less than 0.5 million are "small"
*/
var _ = require('lodash');
var worker = function(collection){
return _.forEach(collection, function(item) {
if(item.population > 1){
item.size = 'big';
}else if(item.population < 1 && item.population > 0.5){
item.size = 'med';
}else{
item.size = 'small';
}
});
}
module.exports = worker;
//Exercise 4 [ _.every(collection, [callback=identity]) _.some(collection,[callback=identity])]
/*We have a list of the daily average temperatures of different European towns.
{ Hamburg: [14,15,16,14,18,17,20,22,21,18,19,23],
Munich: [16,17,19,20,21,23,22,21,20,19,24,23],
Madrid: [24,23,20,24,24,23,21,22,24,20,24,22],
Stockholm: [16,14,12,15,13,14,14,12,11,14,15,14],
Warsaw: [17,15,16,18,20,20,21,18,19,18,17,20] }
We want to sort these towns into two groups: "warm" and "hot". "warm" should
be towns that have at least one day with a temperature greater than 19. "hot"
should be towns where the temperature is greater than 19 every day. The result
should look like this:
{ hot: [ "Hottown" ],
warm: [ "Town1", "Town2", "Town3" ] }
*/
var _ = require('lodash');
var worker = function(list){
var hotArr = [],
warmArr = [],
obj = {};
_.forEach(list, function(value, key){
var hot = _.every(value, function(item){
return item >= 19;
});
var warm = _.some(value, function(item){
return item >= 19;
});
if (hot) {
hotArr.push(key);
}
else if (warm) {
warmArr.push(key);
}
});
obj.hot = hotArr
obj.warm = warmArr
return obj;
}
module.exports = worker;
//Exercise 7 [_.reduce(collection, [callback=identity], [accumulator])]
/*We have an Array recording orders made at a store.
[ { article: 1, quantity: 4 },
{ article: 2, quantity: 2 },
{ article: 1, quantity: 5 } ]
As you can see in this example data, "1" was ordered twice. We want
to know the total quantities ordered for each article.
Please write a function that:
* Calculates the total number of orders for each article.
* Sorts the resulting array so that the articles with the highest number of orders are on top
[ { article: 1, total_orders: 9 },
{ article: 2, total_orders: 2 } ]
*/
const _ = require("lodash");
const overview = function (orders) {
var overviewarray = [],
total = 0;
// Group by article
orders = _.groupBy(orders, 'article');
_.forEach(orders, function (item, key) {
key = parseInt(key);
total = 0;
// If only one article
if (item.length === 1) {
total = item[0].quantity;
// Else make sum of all orders
} else {
total = _.reduce(item, function (sum, item) {
return sum + item.quantity;
}, 0);
}
overviewarray.push({
article: key,
total_orders: total
});
});
// Order
overviewarray = _.sortBy(overviewarray, "total_orders").reverse();
return overviewarray;
};
module.exports = overview;
//Exercise 2 [_.sortBy(collection, [iteratees=[_.identity]])]
/*We have an Array of items sold yesterday like this:
[{ article: 41, quantity: 24 },
{ article: 2323, quantity: 2 },
{ article: 655, quantity: 23 }]
Please write a function that will sort these items by quantity from largest to smallest.
*/
var _ = require('lodash');
var worker = function(collection){
var sorted = _.sortBy(collection, 'quantity');
return sorted.reverse();
}
module.exports = worker;
//Exercise 9 [ _.template(text)(data)]
/*To get started, use the template() function to interpolate
a simple var for us:
{ name: "Foo",
login: [ 1407574431, 140753421 ]
}
We want a simple string greeting the user by name and showing the
number of times the user has logged in (every timestamp represents
one login). Your function should return a String like this:
Hello Foo (logins: 2)
*/
var _ = require('lodash');
var worker = function(list){
var str = '';
var temp =
_.template('Hello <%= name %> (logins: <%= logins %>)')({'name' : list.name, 'logins' : list.login.length})
return temp;
}
module.exports = worker;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment