-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommendation.js
70 lines (63 loc) · 2.04 KB
/
recommendation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @author C2-20
* @param {Object} user an object with all the users FSLM values, ranging from -11 to 11
* @param {Array} material an array of objects that contain all information about all the materials. Id, pole values etc.
* @returns The sorted array of all given material, based on the users FSLM scores
*/
function recommendationAlgo(user, materials) {
//Checks if there is user or material object
if (!user || !materials) {
return Error;
}
//Checks for null in user object
const dimensions = Object.values(user);
for (const value of dimensions) if (!value) return Error;
let scoresArray = [];
materials.forEach((material) => {
scoresArray.push(calculateScore(user, material));
});
scoresArray = scoresArray.sort(byPersonalScore);
return scoresArray;
}
/**
* @author C2-20
* Helper function to sort an array, based on .personalScore and sets the highest scoring material first.
*/
function byPersonalScore(a, b) {
if (a.personalScore < b.personalScore) return 1;
if (b.personalScore < a.personalScore) return -1;
return 0;
}
/**
* @author C2-20
* @param {object} user an object with a single users information
* @param {object} material an object with a single material and its values
* @returns the material with a new value called .personalScore which contains a percentage of how will the fit is between the user and the material
*/
function calculateScore(user, material) {
let totalScore = 0,
score = 0,
i = 3;
let materialArray = Object.values(material);
for (const key in user) {
if (user[key] < 0) {
//if the user is on the left side of a dimmension
totalScore += Math.abs(user[key]);
score += Math.abs(user[key]) * materialArray[i];
i++;
} else {
//If the user is on the right side of a dimmension
i++;
totalScore += Math.abs(user[key]);
score += user[key] * materialArray[i];
}
i++;
}
material.personalScore = score / totalScore;
return material;
}
module.exports = {
calculateScore,
byPersonalScore,
recommendationAlgo,
};