-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
}); | ||
|
||
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]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. было бы круто избавиться от этой функции, например храня в newLevelFriends не только имена а сразу объект друга |
||
level: friendsWithLevel[i].level + 1 | ||
} | ||
); | ||
namesOfFriends.push(newLevelFriends[j]); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❗️ Обычно цикл for используется для предсказуемого числа итераций. Тут явно не тот случай. |
||
} | ||
|
||
friendsWithLevel.sort(function (friendOne, friendTwo) { | ||
if (friendOne.level > friendTwo.level) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше написать здесь ошибку |
||
} | ||
this.friendsWithLevel = friendsLevel(friends).filter(function (friend) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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; | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❗️ Свйоство |
||
|
||
/** | ||
* Фильтр друзей-девушек | ||
* @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; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В принципе, можно заменить на
Boolean(friend.best)