Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- var allStudents = [],
- allAnimals,
- allBooks,
- allPeople;
- // function constructor for Student
- function Student(firstName, lastName, mark, age) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.name = firstName + " " + lastName;
- this.mark = mark;
- this.age = age;
- }
- // function constructor for Animal
- function Animal(name, species, numberOfLegs) {
- this.name = name;
- this.species = species;
- this.numberOfLegs = numberOfLegs;
- }
- // function constructor for Book
- function Book(title, author) {
- this.title = title;
- this.author = author;
- }
- // function constructor for Person
- function Person(firstName, lastName) {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- // generating 10 random students
- for (var i = 0; i < 10; i++) {
- var randomFirst = chance.first(),
- randomLast = chance.last(),
- randomMark = chance.natural({min: 2, max: 6}),
- randomAge = chance.age();
- allStudents.push(new Student(randomFirst, randomLast, randomMark, randomAge));
- }
- // adding 10 animals, not so random ;D
- allAnimals = [
- new Animal("Gichka", "ant", 6),
- new Animal("Mincho", "ant", 6),
- new Animal("Spanska", "centipede", 100),
- new Animal("Goshko", "spider", 8),
- new Animal("Chanko", "monkey", 2),
- new Animal("Ventsie", "monkey", 2),
- new Animal("Rocky", "dog", 4),
- new Animal("Jack", "dog", 4),
- new Animal("Jerry", "cat", 4),
- new Animal("Ketty", "cat", 4)
- ];
- // adding 10 books, again not random
- allBooks = [
- new Book("The Colour of Magic", "Terry Pratchett"),
- new Book("Small Gods", "Terry Pratchett"),
- new Book("Good Omens", "Terry Pratchett"),
- new Book("Guards! Guards!", "Terry Pratchett"),
- new Book("Gone with the Wind", "Margaret Mitchell"),
- new Book("Wuthering Heights", "Emily Bronte"),
- new Book("The Great Gatsby", "F. Scott Fitzgerald"),
- new Book("The Little Prince", "Antoine de Saint-Exupéry"),
- new Book("Of Mice and Men", "John Steinbeck"),
- new Book("East of Eden", "John Steinbeck")
- ];
- // adding 10 people
- allPeople = [
- new Person("Georgi", "Alexandrov"),
- new Person("Georgi","Ivanov"),
- new Person("Ivan","Ivanov"),
- new Person("Dimitar", "Georgiev"),
- new Person("Georgi", "Ivanov"),
- new Person("Ivan", "Dimitrov"),
- new Person("Ivan", "Yordanov"),
- new Person("Dimitar", "Nikolov"),
- new Person("Georgi", "Mihaylov"),
- new Person("Georgi", "Kostadinov")
- ];
- // printing the initial array of students
- console.log('These are all the students in the array ');
- console.log(allStudents);
- // ------------------- 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.
- console.log('1. These are the student whose first name is before its last name alphabetically ');
- (function firstBeforeLast() {
- var firstNameBeforeLast = _.filter(allStudents, function (student) {
- if (student.firstName < student.lastName) {
- return true;
- }
- return false;
- });
- var sortedByDescending = _.sortBy(firstNameBeforeLast, "name").reverse();
- _.each(sortedByDescending, function (student) {
- console.log('- ' + student.name);
- });
- })();
- // ------------------- 2. -------------------
- // Write function that finds the first name and last name of
- // all students with age between 18 and 24.
- console.log('2. These are the names of the students between 18 and 24 ');
- (function studentBetween18and24() {
- var studsBetweenGivenAge = _.filter(allStudents, function (student) {
- if (18 <= student.age && student.age <= 24) {
- return true;
- }
- return false;
- });
- if (studsBetweenGivenAge.length > 0) {
- _.each(studsBetweenGivenAge, function (student) {
- console.log('- ' + student.name);
- })
- }
- else {
- console.log(' - There are currently no students between 18 and 24 (refresh and try again ;D )');
- }
- })();
- // ------------------- 3. -------------------
- // Write a function that by a given array of students finds the student with highest marks
- console.log('3. This is the oustanding student (with highest marks) ');
- (function findOustanding() {
- var outstanding = _.max(allStudents, function (student) {
- return student.mark;
- });
- console.log("- " + outstanding.name + " with mark " + outstanding.mark);
- })();
- // printing the initial array of animals
- console.log('These are all the animals in the array ');
- console.log(allAnimals);
- // ------------------- 4. -------------------
- // Write a function that by a given array of animals, groups them by species and sorts them by number of legs
- console.log('4. Animals grouped by species and sorted by number of legs ');
- (function animalsGrouped() {
- var animalsGroupedBySpecies = _.groupBy(allAnimals, 'species');
- var sortedAnimals = _.sortBy(animalsGroupedBySpecies, function(an){
- return an[0].numberOfLegs;
- });
- console.log(sortedAnimals);
- })();
- // ------------------- 5. -------------------
- // By a given array of animals, find the total number of legs
- console.log('5. The total number of legs is ');
- (function findingNumberOfLegs() {
- var numberOfLegs = 0;
- _.each(allAnimals, function (animal) {
- numberOfLegs += animal.numberOfLegs;
- });
- console.log(numberOfLegs);
- })();
- // printing the initial array of books
- console.log('These are all the books in the array ');
- console.log(allBooks);
- // ------------------- 6. -------------------
- // By a given collection of books, find the most popular author
- // (the author with the highest number of books)
- console.log('6. The most popular author is ');
- (function findMostPopularAuthor() {
- var booksByAuthor = _.groupBy(allBooks, 'author');
- var mostPopularBooks = _.max(booksByAuthor, function (books) {
- return books.length;
- });
- console.log("- " + mostPopularBooks[0].author + " with " + mostPopularBooks.length + " books.");
- })();
- // printing the initial array of people
- console.log('These are all the people in the array ');
- console.log(allPeople);
- // ------------------- 7. -------------------
- // By an array of people find the most common first and last name.
- console.log('7. The most common first and last names are ');
- (function findMostCommonNames(){
- var peopleByFname = _.groupBy(allPeople, 'firstName');
- var mostPopularFNames = _.max(peopleByFname, function (names) {
- return names.length;
- });
- console.log('First name: ' + mostPopularFNames[0].firstName);
- var peopleByLname = _.groupBy(allPeople, 'lastName');
- var mostPopularLNames = _.max(peopleByLname, function (names) {
- return names.length;
- });
- console.log('Last name: ' + mostPopularLNames[0].lastName);
- })();
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement