From 81dac191456770815a88fc172684f813a9ff4cd7 Mon Sep 17 00:00:00 2001 From: Nazarova Galina Date: Sun, 20 Nov 2016 19:35:15 +0500 Subject: [PATCH 1/5] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/lib.js b/lib.js index 60f30ab..bafd5fa 100644 --- a/lib.js +++ b/lib.js @@ -1,5 +1,54 @@ 'use strict'; +function friendsLevel(friends) { + var friendsWithLevel = []; + + friends.forEach(function (friend) { + if (friend.hasOwnProperty('best')) { + friendsWithLevel.push( + { + friend: friend, + level: 1 + } + ); + } else { + var minLevel = Infinity; + friend.friends.forEach(function (connectedFriend) { + var friendsName = friendsWithLevel.filter(function (friendWithLevel) { + return friendWithLevel.friend.name === connectedFriend; + }); + if (friendsName.length > 0 && minLevel > friendsName[0].level) { + minLevel = friendsName[0].level + 1; + } + }); + if (minLevel !== Infinity) { + friendsWithLevel.push( + { + friend: friend, + level: minLevel + } + ); + } + } + }); + + friendsWithLevel.sort(function (friendOne, friendTwo) { + if (friendOne.level > friendTwo.level) { + return 1; + } + if (friendOne.level < friendTwo.level) { + return -1; + } + if (friendOne.friend.name > friendTwo.friend.name) { + return 1; + } + + return -1; + }); + + return friendsWithLevel; +} + /** * Итератор по друзьям * @constructor @@ -7,9 +56,28 @@ * @param {Filter} filter */ function Iterator(friends, filter) { - console.info(friends, filter); + if (!Filter.prototype.isPrototypeOf(filter)) { + throw new TypeError(); + } + this.friendsWithLevel = friendsLevel(friends).filter(function (friend) { + 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 @@ -19,15 +87,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; + }; } /** @@ -36,18 +111,26 @@ function Filter() { * @constructor */ function MaleFilter() { - console.info('MaleFilter'); + this.condition = function (friend) { + return friend.gender === 'male'; + }; } +MaleFilter.prototype = Object.create(Filter.prototype); + /** * Фильтр друзей-девушек * @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; From 5f74c15153c1b22a6ebc81b8318234be36f3a3be Mon Sep 17 00:00:00 2001 From: Nazarova Galina Date: Sun, 20 Nov 2016 22:50:58 +0500 Subject: [PATCH 2/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC?= =?UTF-8?q?=20=D0=BF=D0=BE=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B4=D0=B5=D1=80=D0=B5=D0=B2=D0=B0=20=D0=B4=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib.js b/lib.js index bafd5fa..068eed9 100644 --- a/lib.js +++ b/lib.js @@ -1,36 +1,43 @@ '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'); + }); + + var namesOfFriends = []; var friendsWithLevel = []; + bestFriends.forEach(function (friend) { + namesOfFriends.push(friend.name); + friendsWithLevel.push( + { + friend: friend, + level: 1 + } + ); + }); - friends.forEach(function (friend) { - if (friend.hasOwnProperty('best')) { + 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: friend, - level: 1 + friend: getFriendByName(friends, newLevelFriends[j]), + level: friendsWithLevel[i].level + 1 } ); - } else { - var minLevel = Infinity; - friend.friends.forEach(function (connectedFriend) { - var friendsName = friendsWithLevel.filter(function (friendWithLevel) { - return friendWithLevel.friend.name === connectedFriend; - }); - if (friendsName.length > 0 && minLevel > friendsName[0].level) { - minLevel = friendsName[0].level + 1; - } - }); - if (minLevel !== Infinity) { - friendsWithLevel.push( - { - friend: friend, - level: minLevel - } - ); - } + namesOfFriends.push(newLevelFriends[j]); } - }); + } friendsWithLevel.sort(function (friendOne, friendTwo) { if (friendOne.level > friendTwo.level) { From 61c260667baae6d4e53a48425d2ad85957269fac Mon Sep 17 00:00:00 2001 From: Nazarova Galina Date: Sat, 26 Nov 2016 17:21:29 +0500 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=20=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=B8=D0=B5=20done?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib.js b/lib.js index 068eed9..490a75c 100644 --- a/lib.js +++ b/lib.js @@ -9,7 +9,7 @@ function getFriendByName(friends, name) { function friendsLevel(friends) { var bestFriends = friends.filter(function (friend) { - return friend.hasOwnProperty('best'); + return friend.hasOwnProperty('best') && friend.best; }); var namesOfFriends = []; @@ -82,7 +82,7 @@ Iterator.prototype.next = function () { }; Iterator.prototype.done = function () { - return this.currentFriend >= this.friendsWithLevel.length; + return this.currentFriend === this.friendsWithLevel.length; }; /** From c6c45c3b3911339c105a87f6d8b3db87d5a5375e Mon Sep 17 00:00:00 2001 From: Nazarova Galina Date: Sat, 3 Dec 2016 15:25:21 +0500 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=20=D0=B7=D0=B0=D0=BC?= =?UTF-8?q?=D0=B5=D1=87=D0=B0=D0=BD=D0=B8=D1=8F=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 63 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/lib.js b/lib.js index 490a75c..d0f4847 100644 --- a/lib.js +++ b/lib.js @@ -1,12 +1,23 @@ 'use strict'; -function getFriendByName(friends, name) { - return friends.filter(function (friend) { - return friend.name === name; - })[0]; +/** + * Итератор по друзьям + * @constructor + * @param {Object[]} friends + * @param {Filter} filter + */ +function Iterator(friends, filter) { + if (!Filter.prototype.isPrototypeOf(filter)) { + throw new TypeError('Wrong type of filter. It must be Filter'); + } + this.friendsWithLevel = this.friendsLevel(friends).filter(function (friend) { + return filter.condition(friend.friend); + }); + + this.currentFriend = 0; } -function friendsLevel(friends) { +Iterator.prototype.friendsLevel = function (friends) { var bestFriends = friends.filter(function (friend) { return friend.hasOwnProperty('best') && friend.best; @@ -25,17 +36,25 @@ function friendsLevel(friends) { }); for (var i = 0; i < friendsWithLevel.length; i++) { - var newLevelFriends = friendsWithLevel[i].friend.friends.filter(function (friendName) { - return namesOfFriends.indexOf(friendName) === -1; - }); + var newLevelFriends = + friendsWithLevel[i].friend.friends.reduce(function (newLevelFunction, friendName) { + if (namesOfFriends.indexOf(friendName) === -1) { + var friendToPush = friends.filter(function (friend) { + return friend.name === friendName; + }); + newLevelFunction = newLevelFunction.concat(friendToPush); + } + + return newLevelFunction; + }, []); for (var j = 0; j < newLevelFriends.length; j++) { friendsWithLevel.push( { - friend: getFriendByName(friends, newLevelFriends[j]), + friend: newLevelFriends[j], level: friendsWithLevel[i].level + 1 } ); - namesOfFriends.push(newLevelFriends[j]); + namesOfFriends.push(newLevelFriends[j].name); } } @@ -46,32 +65,13 @@ function friendsLevel(friends) { if (friendOne.level < friendTwo.level) { return -1; } - if (friendOne.friend.name > friendTwo.friend.name) { - return 1; - } - return -1; + return (friendOne.friend.name > friendTwo.friend.name) ? 1 : -1; }); return friendsWithLevel; -} - -/** - * Итератор по друзьям - * @constructor - * @param {Object[]} friends - * @param {Filter} filter - */ -function Iterator(friends, filter) { - if (!Filter.prototype.isPrototypeOf(filter)) { - throw new TypeError(); - } - this.friendsWithLevel = friendsLevel(friends).filter(function (friend) { - return filter.condition(friend.friend); - }); +}; - this.currentFriend = 0; -} Iterator.prototype.next = function () { if (this.currentFriend === this.friendsWithLevel.length) { @@ -85,6 +85,7 @@ Iterator.prototype.done = function () { return this.currentFriend === this.friendsWithLevel.length; }; + /** * Итератор по друзям с ограничением по кругу * @extends Iterator From bb5476ddea22e658856cc6c51eae6802b91ff117 Mon Sep 17 00:00:00 2001 From: Nazarova Galina Date: Sat, 3 Dec 2016 18:46:38 +0500 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=20=D0=B7=D0=B0=D0=BC?= =?UTF-8?q?=D0=B5=D1=87=D0=B0=D0=BD=D0=B8=D1=8F=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib.js b/lib.js index d0f4847..e2362ec 100644 --- a/lib.js +++ b/lib.js @@ -18,7 +18,6 @@ function Iterator(friends, filter) { } Iterator.prototype.friendsLevel = function (friends) { - var bestFriends = friends.filter(function (friend) { return friend.hasOwnProperty('best') && friend.best; });