forked from adithengky/nodeSAW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
50 lines (43 loc) · 1.21 KB
/
controller.js
File metadata and controls
50 lines (43 loc) · 1.21 KB
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
const _ = require("lodash");
const criteria = require("./criteria");
const Controller = {};
function getMinMax(value) {
const hargaMin = _.minBy(value, "harga");
const peminatMax = _.maxBy(value, "peminat");
const kemasanMax = _.maxBy(value, "kemasan");
return {
harga: hargaMin.harga,
peminat: peminatMax.peminat,
kemasan: kemasanMax.kemasan
};
}
function normalisasi(matrix, maxmin) {
matrix.harga = maxmin.harga / matrix.harga;
matrix.peminat = matrix.peminat / maxmin.peminat;
matrix.kemasan = matrix.kemasan / maxmin.kemasan;
return matrix;
}
function hitungPeringkat(nilai) {
const total =
nilai.harga * criteria.harga +
nilai.peminat * criteria.peminat +
nilai.kemasan * criteria.kemasan;
const result = {
nama: nilai.nama,
total: total
};
return result;
}
Controller.getRecomendation = (req, res) => {
const post = req.body;
const getNilaiBobot = post;
const getMaxMin = getMinMax(getNilaiBobot);
const normalisasiNilai = _.map(getNilaiBobot, nilai =>
normalisasi(nilai, getMaxMin)
);
const hitungBobotPeringkat = _.map(normalisasiNilai, nilai =>
hitungPeringkat(nilai)
);
res.json(hitungBobotPeringkat);
};
module.exports = Controller;