Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $$ = λ = function(body) {
- return function() {
- var a = arguments;
- var x = arguments[0];
- var y = arguments[1];
- var z = arguments[2];
- return eval("("+body+")");
- };
- };
- // 1 Write a method that from a given array of students finds all students whose first name is before its last name alphabetically. Print the students in descending order by full name. Use Underscore.js
- var protoStudent = {
- fullName: function() { return this.firstName + " " + this.lastName; }
- };
- var makeStudent = function(fname, lname, age, marks) {
- return _.extend(Object.create(protoStudent),
- { firstName: fname, lastName: lname, age: age, marks: marks });
- };
- var makeStudents = function() {
- return _.map(arguments, λ('makeStudent.apply(null,x)'));
- };
- var students = makeStudents(
- ['Joro','Peshev',25,[4,4.5,6,2]],
- ['Aaaaa', 'Zzzzz', 100,[]],
- ['Minka', 'Tosheva', 22,[2.5,6.0,4.0]],
- ['Pesho','Jorev', 30,[6.0,5.5]]
- );
- var task1 = function() {
- var ourStudents = _.chain(students)
- .filter(λ('x.firstName < x.lastName'))
- .sortBy(λ('x.fullName()'))
- .values()
- .reverse();
- _(ourStudents).each(λ('console.log(x.fullName())'));
- };
- // 2 Write function that finds the first name and last name of all students with age between 18 and 24. Use Underscore.js
- var task2 = function() {
- return _.chain(students)
- .filter(λ('18 <= x.age && x.age <= 24'))
- .map(λ('x.fullName()'))
- .value();
- };
- // 3 Write a function that by a given array of students finds the student with highest marks
- var average = function(arr) {
- return _.reduce(arr, λ('x+y'), 0) / arr.length;
- };
- var task3 = function() {
- return _(students)
- .max(λ('average(x.marks)'));
- };
- // 4 Write a function that by a given array of animals, groups them by species and sorts them by number of legs
- var makeAnimals = function() {
- return _(arguments)
- .map(λ('{ name: x[0], species: x[1], legs: x[2] }'));
- };
- var animals = makeAnimals(
- ["ginger", "rabbit", 4],
- ["winner", "dog", 4],
- ["chuck", "chicken", 2],
- ["chernobyl", "chicken", 6],
- ["harold", "spider", 8],
- ["smith", "pig", 4],
- ["samuel", "octopus", 8],
- ["horace the survivor", "pig", 3]
- );
- var task4 = function() {
- var groups = _(animals).groupBy(λ('x.species'));
- // sort animals in each group?
- // var sortedGroups = _(groups).map(λ('_(x).sortBy(λ("x.legs"))'));
- // sort all groups?
- var sortedGroups = _(groups).sortBy(λ('x[0].legs'));
- return sortedGroups;
- };
- // 5 By a given array of animals, find the total number of legs
- // Each animal can have 2, 4, 6, 8 or 100 legs
- var task5 = function() {
- return _.chain(animals)
- .pluck('legs')
- .reduce(λ('x+y'), 0)
- .value();
- };
- // 6 By a given collection of books, find the most popular author (the author with the biggest number of books)
- var books = _.map(
- [["Stephen King", "The Dark Tower"],
- ["George Orwell", "Animal Farm"],
- ["William Shakespeare", "Much Ado About Nothing"],
- ["George Orwell", "Homage to Catalonia"],
- ["Robert Heinlein", "Starship Troopers"]],
- λ('{author: x[0], title: x[1]}'));
- var task6 = function() {
- return _.chain(books)
- .groupBy(λ('x.author'))
- .max(λ('x.length'))
- .value()[0].author;
- };
- // 7 By an array of people find the most common first and last name. Use underscore.
- var people = makeStudents(
- ['Joro','Peshev'],
- ['Minka', 'Tosheva'],
- ['Pesho','Jorev'],
- ['Joro','Goshev'],
- ['Joro', 'Mishev'],
- ['Pesho','Goshev'],
- ['Minkia','Gosheva'],
- ['Pesho','Peshev'],
- ['Gosho','Mishev'],
- ['Joro','Mishev']
- );
- var task7 = function() {
- var firstName =
- _.chain(people)
- .groupBy(λ('x.firstName'))
- .max(λ('x.length'))
- .value()[0].firstName;
- var lastName =
- _.chain(people)
- .groupBy(λ('x.lastName'))
- .max(λ('x.length'))
- .value()[0].lastName;
- return [firstName, lastName];
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement