Advertisement
lina94

JSApps Underscore HW

Jul 20th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     (function () {
  2.         var allStudents = [],
  3.                 allAnimals,
  4.                 allBooks,
  5.                 allPeople;
  6.  
  7.         // function constructor for Student
  8.         function Student(firstName, lastName, mark, age) {
  9.             this.firstName = firstName;
  10.             this.lastName = lastName;
  11.             this.name = firstName + " " + lastName;
  12.             this.mark = mark;
  13.             this.age = age;
  14.         }
  15.  
  16.         // function constructor for Animal
  17.         function Animal(name, species, numberOfLegs) {
  18.             this.name = name;
  19.             this.species = species;
  20.             this.numberOfLegs = numberOfLegs;
  21.         }
  22.  
  23.         // function constructor for Book
  24.         function Book(title, author) {
  25.             this.title = title;
  26.             this.author = author;
  27.         }
  28.  
  29.         // function constructor for Person
  30.         function Person(firstName, lastName) {
  31.             this.firstName = firstName;
  32.             this.lastName = lastName;
  33.         }
  34.  
  35.         // generating 10 random students
  36.         for (var i = 0; i < 10; i++) {
  37.             var randomFirst = chance.first(),
  38.                     randomLast = chance.last(),
  39.                     randomMark = chance.natural({min: 2, max: 6}),
  40.                     randomAge = chance.age();
  41.  
  42.             allStudents.push(new Student(randomFirst, randomLast, randomMark, randomAge));
  43.         }
  44.  
  45.         // adding 10 animals, not so random ;D
  46.         allAnimals = [
  47.             new Animal("Gichka", "ant", 6),
  48.             new Animal("Mincho", "ant", 6),
  49.             new Animal("Spanska", "centipede", 100),
  50.             new Animal("Goshko", "spider", 8),
  51.             new Animal("Chanko", "monkey", 2),
  52.             new Animal("Ventsie", "monkey", 2),
  53.             new Animal("Rocky", "dog", 4),
  54.             new Animal("Jack", "dog", 4),
  55.             new Animal("Jerry", "cat", 4),
  56.             new Animal("Ketty", "cat", 4)
  57.         ];
  58.  
  59.         // adding 10 books, again not random
  60.         allBooks = [
  61.             new Book("The Colour of Magic", "Terry Pratchett"),
  62.             new Book("Small Gods", "Terry Pratchett"),
  63.             new Book("Good Omens", "Terry Pratchett"),
  64.             new Book("Guards! Guards!", "Terry Pratchett"),
  65.             new Book("Gone with the Wind", "Margaret Mitchell"),
  66.             new Book("Wuthering Heights", "Emily Bronte"),
  67.             new Book("The Great Gatsby", "F. Scott Fitzgerald"),
  68.             new Book("The Little Prince", "Antoine de Saint-Exupéry"),
  69.             new Book("Of Mice and Men", "John Steinbeck"),
  70.             new Book("East of Eden", "John Steinbeck")
  71.         ];
  72.  
  73.         // adding 10 people
  74.         allPeople = [
  75.             new Person("Georgi", "Alexandrov"),
  76.             new Person("Georgi","Ivanov"),
  77.             new Person("Ivan","Ivanov"),
  78.             new Person("Dimitar", "Georgiev"),
  79.             new Person("Georgi", "Ivanov"),
  80.             new Person("Ivan", "Dimitrov"),
  81.             new Person("Ivan", "Yordanov"),
  82.             new Person("Dimitar", "Nikolov"),
  83.             new Person("Georgi", "Mihaylov"),
  84.             new Person("Georgi", "Kostadinov")
  85.         ];
  86.  
  87.         // printing the initial array of students
  88.         console.log('These are all the students in the array ');
  89.         console.log(allStudents);
  90.  
  91.         // -------------------   1.   -------------------
  92.         // Write a method that from a given array of students
  93.         // finds all students whose first name is before its last name alphabetically.
  94.         // Print the students in descending order by full name.
  95.         console.log('1. These are the student whose first name is before its last name alphabetically ');
  96.  
  97.         (function firstBeforeLast() {
  98.             var firstNameBeforeLast = _.filter(allStudents, function (student) {
  99.                 if (student.firstName < student.lastName) {
  100.                     return true;
  101.                 }
  102.                 return false;
  103.             });
  104.  
  105.             var sortedByDescending = _.sortBy(firstNameBeforeLast, "name").reverse();
  106.             _.each(sortedByDescending, function (student) {
  107.                 console.log('- ' + student.name);
  108.             });
  109.         })();
  110.  
  111.         // -------------------   2.   -------------------
  112.         // Write function that finds the first name and last name of
  113.         // all students with age between 18 and 24.
  114.         console.log('2. These are the names of the students between 18 and 24 ');
  115.  
  116.         (function studentBetween18and24() {
  117.             var studsBetweenGivenAge = _.filter(allStudents, function (student) {
  118.                 if (18 <= student.age && student.age <= 24) {
  119.                     return true;
  120.                 }
  121.                 return false;
  122.             });
  123.             if (studsBetweenGivenAge.length > 0) {
  124.                 _.each(studsBetweenGivenAge, function (student) {
  125.                     console.log('- ' + student.name);
  126.                 })
  127.             }
  128.             else {
  129.                 console.log(' - There are currently no students between 18 and 24 (refresh and try again ;D )');
  130.             }
  131.         })();
  132.  
  133.         // -------------------   3.   -------------------
  134.         // Write a function that by a given array of students finds the student with highest marks
  135.         console.log('3. This is the oustanding student (with highest marks) ');
  136.  
  137.         (function findOustanding() {
  138.             var outstanding = _.max(allStudents, function (student) {
  139.                 return student.mark;
  140.             });
  141.             console.log("- " + outstanding.name + " with mark " + outstanding.mark);
  142.         })();
  143.  
  144.  
  145.         // printing the initial array of animals
  146.         console.log('These are all the animals in the array ');
  147.         console.log(allAnimals);
  148.  
  149.         // -------------------   4.   -------------------
  150.         // Write a function that by a given array of animals, groups them by species and sorts them by number of legs
  151.         console.log('4. Animals grouped by species and sorted by number of legs ');
  152.  
  153.         (function animalsGrouped() {
  154.             var animalsGroupedBySpecies = _.groupBy(allAnimals, 'species');
  155.                var sortedAnimals = _.sortBy(animalsGroupedBySpecies, function(an){
  156.                    return an[0].numberOfLegs;
  157.                });
  158.  
  159.             console.log(sortedAnimals);
  160.         })();
  161.  
  162.         // -------------------   5.   -------------------
  163.         // By a given array of animals, find the total number of legs
  164.         console.log('5. The total number of legs is ');
  165.  
  166.         (function findingNumberOfLegs() {
  167.             var numberOfLegs = 0;
  168.  
  169.             _.each(allAnimals, function (animal) {
  170.                 numberOfLegs += animal.numberOfLegs;
  171.             });
  172.  
  173.             console.log(numberOfLegs);
  174.         })();
  175.  
  176.         // printing the initial array of books
  177.         console.log('These are all the books in the array ');
  178.         console.log(allBooks);
  179.  
  180.         // -------------------   6.   -------------------
  181.         // By a given collection of books, find the most popular author
  182.         // (the author with the highest number of books)
  183.         console.log('6. The most popular author is ');
  184.  
  185.         (function findMostPopularAuthor() {
  186.             var booksByAuthor = _.groupBy(allBooks, 'author');
  187.             var mostPopularBooks = _.max(booksByAuthor, function (books) {
  188.                 return books.length;
  189.             });
  190.  
  191.             console.log("- " + mostPopularBooks[0].author + " with " + mostPopularBooks.length + " books.");
  192.         })();
  193.  
  194.         // printing the initial array of people
  195.         console.log('These are all the people in the array ');
  196.         console.log(allPeople);
  197.  
  198.         // -------------------   7.   -------------------
  199.         // By an array of people find the most common first and last name.
  200.         console.log('7. The most common first and last names are ');
  201.  
  202.         (function findMostCommonNames(){
  203.             var peopleByFname = _.groupBy(allPeople, 'firstName');
  204.             var mostPopularFNames = _.max(peopleByFname, function (names) {
  205.                 return names.length;
  206.             });
  207.             console.log('First name: ' + mostPopularFNames[0].firstName);
  208.  
  209.             var peopleByLname = _.groupBy(allPeople, 'lastName');
  210.             var mostPopularLNames = _.max(peopleByLname, function (names) {
  211.                 return names.length;
  212.             });
  213.             console.log('Last name: ' + mostPopularLNames[0].lastName);
  214.         })();
  215.  
  216.     }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement