This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
solution-comparison-vue.js
83 lines (78 loc) · 2.16 KB
/
solution-comparison-vue.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
class Solution {
constructor(value) {
this.value = value;
this.satisfiedRequirements = [];
}
get score() {
return this.satisfiedRequirements.map(requirement => requirement.score).reduce((sum, score) => sum + score, 0);
}
satisfies(requirement) {
this.satisfiedRequirements.push(requirement);
}
notSatisfies(requirement) {
for (let i = 0; i < this.satisfiedRequirements.length; i++) {
if (this.satisfiedRequirements[i] === requirement) {
this.satisfiedRequirements.splice(i, 1);
i--;
}
}
}
}
const app = new Vue({
el: '#app',
data: {
newRequirementValue: '',
newSolutionValue: '',
requirements: [],
requirementPairs: [],
solutions: []
},
methods: {
addRequirement: function () {
if (!this.newRequirementValue) {
return;
}
const newRequirement = new Item(this.newRequirementValue, 1);
for (const requirement of this.requirements) {
this.requirementPairs.push(new Pair(requirement, newRequirement));
}
this.requirements.push(newRequirement);
this.newRequirementValue = '';
},
addSolution: function () {
if (!this.newSolutionValue) {
return;
}
this.solutions.push(new Solution(this.newSolutionValue));
this.newSolutionValue = '';
},
evaluateSolution: function (e, solution, requirement) {
if (e.target.checked) {
solution.satisfies(requirement);
} else {
solution.notSatisfies(requirement)
}
}
},
computed: {
sortedRequirements: function () {
return Array.from(this.requirements).sort((a, b) => b.score - a.score);
},
notVotedPairs: function () {
return this.requirementPairs.filter(pair => !pair.voted);
},
sortedSolutions: function () {
return Array.from(this.solutions).sort((a, b) => b.score - a.score);
},
maxScore: function () {
return Math.max.apply(null, this.solutions.map(solution => solution.score));
},
winners: function () {
if (this.maxScore > 1) {
return this.solutions.filter(item => item.score == this.maxScore);
} else {
return [];
}
},
}
});