Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Назарова Галина #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 95 additions & 5 deletions lib.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,90 @@
'use strict';

function getFriendByName(friends, name) {
return friends.filter(function (friend) {
return friend.name === name;
})[0];
}

function friendsLevel(friends) {

var bestFriends = friends.filter(function (friend) {
return friend.hasOwnProperty('best') && friend.best;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В принципе, можно заменить на Boolean(friend.best)

});

var namesOfFriends = [];
var friendsWithLevel = [];
bestFriends.forEach(function (friend) {
namesOfFriends.push(friend.name);
friendsWithLevel.push(
{
friend: friend,
level: 1
}
);
});

for (var i = 0; i < friendsWithLevel.length; i++) {
var newLevelFriends = friendsWithLevel[i].friend.friends.filter(function (friendName) {
return namesOfFriends.indexOf(friendName) === -1;
});
for (var j = 0; j < newLevelFriends.length; j++) {
friendsWithLevel.push(
{
friend: getFriendByName(friends, newLevelFriends[j]),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

было бы круто избавиться от этой функции, например храня в newLevelFriends не только имена а сразу объект друга

level: friendsWithLevel[i].level + 1
}
);
namesOfFriends.push(newLevelFriends[j]);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ Обычно цикл for используется для предсказуемого числа итераций. Тут явно не тот случай.
Я бы предложил использовать очередь, из которой выталкивать по одному элементу. Имеется фор по friendsWithLevel

}

friendsWithLevel.sort(function (friendOne, friendTwo) {
if (friendOne.level > friendTwo.level) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно красиво сделать через тернарник

return 1;
}
if (friendOne.level < friendTwo.level) {
return -1;
}
if (friendOne.friend.name > friendTwo.friend.name) {
return 1;
}

return -1;
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ А можно без этой сортировки в конце обойтись?


return friendsWithLevel;
}

/**
* Итератор по друзьям
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
*/
function Iterator(friends, filter) {
console.info(friends, filter);
if (!Filter.prototype.isPrototypeOf(filter)) {
throw new TypeError();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше написать здесь ошибку

}
this.friendsWithLevel = friendsLevel(friends).filter(function (friend) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

все методы нужно разместить в прототипах

return filter.condition(friend.friend);
});

this.currentFriend = 0;
}

Iterator.prototype.next = function () {
if (this.currentFriend === this.friendsWithLevel.length) {
return null;
}

return this.friendsWithLevel[this.currentFriend++].friend;
};

Iterator.prototype.done = function () {
return this.currentFriend === this.friendsWithLevel.length;
};

/**
* Итератор по друзям с ограничением по кругу
* @extends Iterator
Expand All @@ -19,15 +94,22 @@ function Iterator(friends, filter) {
* @param {Number} maxLevel – максимальный круг друзей
*/
function LimitedIterator(friends, filter, maxLevel) {
console.info(friends, filter, maxLevel);
Iterator.call(this, friends, filter);
this.friendsWithLevel = this.friendsWithLevel.filter(function (friend) {
return maxLevel >= friend.level;
});
}

LimitedIterator.prototype = Object.create(Iterator.prototype);

/**
* Фильтр друзей
* @constructor
*/
function Filter() {
console.info('Filter');
this.condition = function () {
return true;
};
}

/**
Expand All @@ -36,18 +118,26 @@ function Filter() {
* @constructor
*/
function MaleFilter() {
console.info('MaleFilter');
this.condition = function (friend) {
return friend.gender === 'male';
};
}

MaleFilter.prototype = Object.create(Filter.prototype);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ Свйоство (new MaleFilter()).contructor теперь указывает на Filter


/**
* Фильтр друзей-девушек
* @extends Filter
* @constructor
*/
function FemaleFilter() {
console.info('FemaleFilter');
this.condition = function (friend) {
return friend.gender === 'female';
};
}

FemaleFilter.prototype = Object.create(Filter.prototype);

exports.Iterator = Iterator;
exports.LimitedIterator = LimitedIterator;

Expand Down