var properties = ['weight', 'height'];
var sum = function (values) {
return _.reduce(values, function (a, b) { return a + b;});
};
var getSquaredDifference = function (a, b) {
return Math.pow(a - b, 2);
};
var getDateDifference = function(a, b) {
return Math.abs(new Date(a) - new Date(b));
};
var setDissimilarityDistance = function (athletes, user) {
_.each(athletes, function (athlete) {
var distance;
var squareDifferences = [];
_.each(properties, function (prop) {
squareDifferences.push(getSquaredDifference(athlete[prop], user[prop]));
});
athlete['distance'] = sum(squareDifferences);
athlete['birthDifference'] = getDateDifference(athlete.dob, user.dob);
});
return athletes;
};
var getBodyMatches = function (athletes, user) {
var dataWithDistances = setDissimilarityDistance(athletes, user);
var athleteMatches = _.sortBy(_.sortBy(dataWithDistances, 'birthDifference'), 'distance');
return athletesMatches.slice(0,3);
};