SHARE
TWEET

Untitled

a guest Aug 21st, 2013 620 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <reference path="class.js" />
  2. /// <reference path="underscore-min.js" />
  3. (function () {
  4.     //task 1 Write a method that from a given array of students finds all students whose
  5.     //first name is before its last name alphabetically. Print the students in descending order by
  6.     //full name. Use Underscore.js
  7.  
  8.     var Student = Class.create({
  9.         init: function (fname, lname, age, mark) {
  10.             this.fname = fname;
  11.             this.lname = lname;
  12.             this.age = age;
  13.             this.mark = mark;
  14.         },
  15.         toString: function () {
  16.             return this.fname + ' ' + this.lname + ' ' + this.age + ' ' + this.mark;
  17.         }
  18.     });
  19.  
  20.     var Animal = Class.create({
  21.         init: function (fname, species, legs) {
  22.             this.fname = fname;
  23.             this.species = species;
  24.             this.legs = legs;
  25.         },
  26.         toString: function () {
  27.             return this.fname + ' ' + this.species + ' ' + this.legs;
  28.         }
  29.     });
  30.  
  31.     var Book = Class.create({
  32.         init: function (name, author) {
  33.             this.name = name,
  34.             this.author = author;
  35.         },
  36.         toString: function () {
  37.             return this.name + ' ' + this.author;
  38.         }
  39.     });
  40.  
  41.     var books = [
  42.         new Book("hodi mi se na more", "ByMe"),
  43.         new Book("trqbva mi relaxacia", "ByMe"),
  44.         new Book("Nqma da pochivash", "AcademyAndCo"),
  45.         new Book("Sleep is for the weak", "AcademyAndCo"),
  46.         new Book("Get over it", "AcademyAndCo")
  47.     ];
  48.  
  49.     var animals = [
  50.             new Animal("Kozata Spaska", "goat", 4),
  51.             new Animal("Kozata Troshka", "goat", 4),
  52.             new Animal("Kozata Fashka", "goat", 4),
  53.             new Animal("Maimunata Miska", "monkey", 2),
  54.             new Animal("Maimunata Pushka", "monkey", 2),
  55.             new Animal("Ednonogo", "onemanshow", 1),
  56.             new Animal("Ednonogo2", "onemanshow", 1),
  57.     ];
  58.  
  59.     var students = [
  60.         new Student("Kiril", "Marichkov", 18, 2),
  61.         new Student("Kiril", "Lioliov", 10, 6),
  62.         new Student("Nasko", "Kaskata", 24, 7),
  63.         new Student("Misho", "Kalyfa", 26, 3),
  64.         new Student("Lakomcho", "Shtaigata", 30, 5)
  65.     ];
  66.  
  67.     var people = [
  68.         new Student("Angel", "Metala", 0, 0),
  69.         new Student("Angel", "Metala", 0, 0),
  70.         new Student("Angel", "Metala", 0, 0),
  71.         new Student("Angel", "Metala", 0, 0),
  72.         new Student("Pesho", "Kaskata", 0, 0),
  73.         new Student("Stefan", "Kaskata", 0, 0),
  74.         new Student("Pesho", "Kaskata", 0, 0),
  75.         new Student("Pesho", "Kaskata", 0, 0),
  76.         new Student("Pesho", "Kaskata", 0, 0),
  77.         new Student("Pesho", "Kaskata", 0, 0),
  78.  
  79.     ];
  80.     var filteredStudents = _.filter(students, function (student) {
  81.         return student.fname < student.lname;
  82.     });
  83.  
  84.     //descending by lastName sort in underscore.js
  85.     //filteredStudents = _.sortBy(filteredStudents, function (student) {
  86.     //    var str = student.lname
  87.     //    str = str.toLowerCase();
  88.     //    str = str.split("");
  89.     //    str = _.map(str, function (letter) {
  90.     //        return String.fromCharCode(-(letter.charCodeAt(0)));
  91.     //    });
  92.     //    return str;
  93.     //});
  94.     filteredStudents = _.sortBy(filteredStudents, function (student) { return student.fname + ' ' + student.lname });
  95.  
  96.     filteredStudents = filteredStudents.reverse();
  97.  
  98.     _.each(filteredStudents, function (item) {
  99.         console.log(item.toString());
  100.     });
  101.  
  102.     //task 2 Write function that finds the first name and last name of
  103.     //all students with age between 18 and 24. Use Underscore.js
  104.     console.log('===========================');
  105.     var ageStudents = _.filter(students, function (s) { return s.age >= 18 && s.age <= 24 });
  106.     _.each(ageStudents, function (item) {
  107.         console.log(item.toString());
  108.     });
  109.  
  110.     //task 3 Write a function that by a given array of students finds the student with highest marks
  111.     console.log('===========================');
  112.     var markStudents = _.sortBy(students, function (s) { return s.mark });
  113.     markStudents = _.last(markStudents);
  114.     console.log(markStudents.toString());
  115.  
  116.     //task 4 Write a function that by a given array of animals, groups them by species and sorts them by number of legs
  117.     console.log('===========================');
  118.  
  119.     var groupedAnimals = _.groupBy(animals, 'species');
  120.     groupedAnimals = _.sortBy(groupedAnimals, function (g) { return g[0].legs });
  121.     console.log(groupedAnimals);
  122.  
  123.     //task 5 By a given array of animals, find the total number of legs
  124.     console.log('===========================');
  125.     var legsAnimals = _.reduce(animals, function (memo, a) { return memo + a.legs }, 0);
  126.     console.log(legsAnimals);
  127.  
  128.     //task 6. By a given collection of books, find the most popular author (the author with
  129.     //the biggest number of books)
  130.     console.log('===========================');
  131.     var mostFamousBooks = _.groupBy(books, 'author');
  132.     mostFamousBooks = _.sortBy(mostFamousBooks, function (g) { return -g.length });
  133.     console.log(_.first(mostFamousBooks)[0].author);
  134.  
  135.     //task 7. By an array of people find the most common first and last name. Use underscore.
  136.     console.log('===========================');
  137.  
  138.     //firstName
  139.     var mostCommonFirstName = _.countBy(people, function (person) { return person.fname });
  140.     var index = _.max(mostCommonFirstName);
  141.     mostCommonFirstName = _.invert(mostCommonFirstName);
  142.     console.log(mostCommonFirstName[index]);
  143.  
  144.     //lastName
  145.     var mostCommonLastName = _.countBy(people, function (person) { return person.lname });
  146.     var indexLastName = _.max(mostCommonLastName);
  147.     mostCommonLastName = _.invert(mostCommonLastName);
  148.     console.log(mostCommonLastName[indexLastName]);
  149. }());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top