-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
94 lines (75 loc) · 2.36 KB
/
logic.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import questions from "./questions.js";
import pokemon from "./pokemon.js";
function calculateUserScores(responses) {
let scores = {
Onesta: 0,
Emotivita: 0,
Estroversione: 0,
Amicalita: 0,
Coscienziosita: 0,
Apertura: 0,
};
const count = { ...scores };
// Calcola punteggi e conteggi
responses.forEach((response, index) => {
const question = questions[index];
if (!question) {
console.error("No question data available at index:", index);
return;
}
const questionTrait = question.trait;
let increment = response;
// Inverti il punteggio se la domanda è invertita
if (question.reverse) {
increment = 6 - increment;
}
// Incrementa il punteggio per il tratto specifico
scores[questionTrait] += increment;
count[questionTrait] += 1;
});
// Normalizza i punteggi in base al numero di domande per tratto
Object.keys(scores).forEach((trait) => {
if (count[trait] > 0) {
scores[trait] /= count[trait];
}
console.log(`Punteggio normalizzato per ${trait}: ${scores[trait].toFixed(2)}`);
});
return scores;
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // Scambia gli elementi
}
}
function findBestMatch(userScores) {
let topMatches = [];
let highestScore = -Infinity;
const scoreThreshold = 0.3;
pokemon.forEach((p) => {
let compatibilityScore = 0;
p.traits.forEach((trait) => {
let score = userScores[trait.name] || 0;
// Inverti il punteggio se il tratto non è positivo
if (!trait.positive) {
score = 6 - score;
}
compatibilityScore += score * trait.weight;
});
console.log(`Compatibilità con ${p.name}: ${compatibilityScore.toFixed(2)}`);
if (compatibilityScore > highestScore) {
highestScore = compatibilityScore;
topMatches = [p];
} else if (compatibilityScore >= highestScore - scoreThreshold) {
topMatches.push(p);
}
});
// Garantisci diversità nella selezione
if (topMatches.length > 10) {
shuffleArray(topMatches);
topMatches = topMatches.slice(0, 10);
}
// Ritorna un abbinamento casuale dai migliori
return topMatches[Math.floor(Math.random() * topMatches.length)];
}
export { calculateUserScores, findBestMatch };