Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <reference path="class.js" />
- /// <reference path="underscore-min.js" />
- (function () {
- //task 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 Student = Class.create({
- init: function (fname, lname, age, mark) {
- this.fname = fname;
- this.lname = lname;
- this.age = age;
- this.mark = mark;
- },
- toString: function () {
- return this.fname + ' ' + this.lname + ' ' + this.age + ' ' + this.mark;
- }
- });
- var Animal = Class.create({
- init: function (fname, species, legs) {
- this.fname = fname;
- this.species = species;
- this.legs = legs;
- },
- toString: function () {
- return this.fname + ' ' + this.species + ' ' + this.legs;
- }
- });
- var Book = Class.create({
- init: function (name, author) {
- this.name = name,
- this.author = author;
- },
- toString: function () {
- return this.name + ' ' + this.author;
- }
- });
- var books = [
- new Book("hodi mi se na more", "ByMe"),
- new Book("trqbva mi relaxacia", "ByMe"),
- new Book("Nqma da pochivash", "AcademyAndCo"),
- new Book("Sleep is for the weak", "AcademyAndCo"),
- new Book("Get over it", "AcademyAndCo")
- ];
- var animals = [
- new Animal("Kozata Spaska", "goat", 4),
- new Animal("Kozata Troshka", "goat", 4),
- new Animal("Kozata Fashka", "goat", 4),
- new Animal("Maimunata Miska", "monkey", 2),
- new Animal("Maimunata Pushka", "monkey", 2),
- new Animal("Ednonogo", "onemanshow", 1),
- new Animal("Ednonogo2", "onemanshow", 1),
- ];
- var students = [
- new Student("Kiril", "Marichkov", 18, 2),
- new Student("Kiril", "Lioliov", 10, 6),
- new Student("Nasko", "Kaskata", 24, 7),
- new Student("Misho", "Kalyfa", 26, 3),
- new Student("Lakomcho", "Shtaigata", 30, 5)
- ];
- var people = [
- new Student("Angel", "Metala", 0, 0),
- new Student("Angel", "Metala", 0, 0),
- new Student("Angel", "Metala", 0, 0),
- new Student("Angel", "Metala", 0, 0),
- new Student("Pesho", "Kaskata", 0, 0),
- new Student("Stefan", "Kaskata", 0, 0),
- new Student("Pesho", "Kaskata", 0, 0),
- new Student("Pesho", "Kaskata", 0, 0),
- new Student("Pesho", "Kaskata", 0, 0),
- new Student("Pesho", "Kaskata", 0, 0),
- ];
- var filteredStudents = _.filter(students, function (student) {
- return student.fname < student.lname;
- });
- //descending by lastName sort in underscore.js
- //filteredStudents = _.sortBy(filteredStudents, function (student) {
- // var str = student.lname
- // str = str.toLowerCase();
- // str = str.split("");
- // str = _.map(str, function (letter) {
- // return String.fromCharCode(-(letter.charCodeAt(0)));
- // });
- // return str;
- //});
- filteredStudents = _.sortBy(filteredStudents, function (student) { return student.fname + ' ' + student.lname });
- filteredStudents = filteredStudents.reverse();
- _.each(filteredStudents, function (item) {
- console.log(item.toString());
- });
- //task 2 Write function that finds the first name and last name of
- //all students with age between 18 and 24. Use Underscore.js
- console.log('===========================');
- var ageStudents = _.filter(students, function (s) { return s.age >= 18 && s.age <= 24 });
- _.each(ageStudents, function (item) {
- console.log(item.toString());
- });
- //task 3 Write a function that by a given array of students finds the student with highest marks
- console.log('===========================');
- var markStudents = _.sortBy(students, function (s) { return s.mark });
- markStudents = _.last(markStudents);
- console.log(markStudents.toString());
- //task 4 Write a function that by a given array of animals, groups them by species and sorts them by number of legs
- console.log('===========================');
- var groupedAnimals = _.groupBy(animals, 'species');
- groupedAnimals = _.sortBy(groupedAnimals, function (g) { return g[0].legs });
- console.log(groupedAnimals);
- //task 5 By a given array of animals, find the total number of legs
- console.log('===========================');
- var legsAnimals = _.reduce(animals, function (memo, a) { return memo + a.legs }, 0);
- console.log(legsAnimals);
- //task 6. By a given collection of books, find the most popular author (the author with
- //the biggest number of books)
- console.log('===========================');
- var mostFamousBooks = _.groupBy(books, 'author');
- mostFamousBooks = _.sortBy(mostFamousBooks, function (g) { return -g.length });
- console.log(_.first(mostFamousBooks)[0].author);
- //task 7. By an array of people find the most common first and last name. Use underscore.
- console.log('===========================');
- //firstName
- var mostCommonFirstName = _.countBy(people, function (person) { return person.fname });
- var index = _.max(mostCommonFirstName);
- mostCommonFirstName = _.invert(mostCommonFirstName);
- console.log(mostCommonFirstName[index]);
- //lastName
- var mostCommonLastName = _.countBy(people, function (person) { return person.lname });
- var indexLastName = _.max(mostCommonLastName);
- mostCommonLastName = _.invert(mostCommonLastName);
- console.log(mostCommonLastName[indexLastName]);
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement