Slide 78
Slide 78 text
@JoeKarlsson1
process.stdout.on('data', data => {
let currWeight = parseFloat(data);
let avgWeight = updateAverageWeight(currWeight, recentsWeights);
});
const checkIfCatIsPresent = (avgWeight, baseBoxWeight) => {
if (avgWeight - bufferWeight > baseBoxWeight + config.cat.weight) {
return true;
}
return false;
};
const updateAverageWeight = (currWeight, recentsWeights) => {
if (currWeight) {
const maxArrLength = 20;
if (recentsWeights.length >= maxArrLength) {
recentsWeights.shift();
}
recentsWeights.push(currWeight);
return getAvg(recentsWeights);
}
};
const getAvg = arr => {
if (arr.length) {
const sum = arr.reduce(function(a, b) {
return a + b;
}, 0);
const avg = sum / arr.length;
return avg;
}
return arr[0];
};
const catPresent = checkIfCatIsPresent(avgWeight, baseBoxWeight);
if (catPresent) {
// Send data to MongoDB
}