Compare commits

..

2 Commits

Author SHA1 Message Date
Timon Ringwald
710080fb89 show < sign instead of rounding to 0 2022-09-09 17:44:41 +02:00
Timon Ringwald
1d0ad7f222 removed logs 2022-09-09 17:22:52 +02:00
2 changed files with 42 additions and 33 deletions

65
main.js
View File

@ -114,7 +114,7 @@ async function addMenuItem(food) {
nodeRoot.setAttribute("data-food-name", food.name); nodeRoot.setAttribute("data-food-name", food.name);
itemName.textContent = food.name; itemName.textContent = food.name;
itemKcal.textContent = `${nutritions100.kcal} kcal/100${food.unit}`; itemKcal.textContent = `${formatValue(nutritions100.kcal)} kcal/100${food.unit}`;
itemUnit.textContent = food.unit; itemUnit.textContent = food.unit;
fatAmount.textContent = `${food.fat}g`; fatAmount.textContent = `${food.fat}g`;
@ -163,23 +163,36 @@ async function addMenuItem(food) {
function updateFoodAmount(food, amount, kcalAmount, fatAmount, carbsAmount, sugarAmount, proteinsAmount, alcAmount) { function updateFoodAmount(food, amount, kcalAmount, fatAmount, carbsAmount, sugarAmount, proteinsAmount, alcAmount) {
const {fat, carbs, sugar, proteins, alc, kcal} = calculateKcal(food, amount); const {fat, carbs, sugar, proteins, alc, kcal} = calculateKcal(food, amount);
kcalAmount.textContent = `${parseInt(kcal)}kcal`; setAmount(kcalAmount, kcal, "kcal");
fatAmount.textContent = `${parseInt(fat)}g`; setAmount(fatAmount, fat, "g");
carbsAmount.textContent = `${parseInt(carbs)}g`; setAmount(carbsAmount, carbs, "g");
sugarAmount.textContent = `${parseInt(sugar)}g`; setAmount(sugarAmount, sugar, "g");
proteinsAmount.textContent = `${parseInt(proteins)}g`; setAmount(proteinsAmount, proteins, "g");
alcAmount.textContent = `${parseInt(alc)}g`; setAmount(alcAmount, alc, "g");
console.log(`${food.name} has ${food.kcal}kcal/100${food.unit}`)
updateSummary(); updateSummary();
} }
function setAmount(element, amount, unit) {
element.textContent = `${formatValue(amount)}${unit}`;
element.setAttribute("data-value", amount);
}
function formatValue(value) {
if (value > 0 && value < 1) {
return "<1"
}
return Math.round(value);
}
function calculateKcal(food, amount) { function calculateKcal(food, amount) {
const fat = Math.round(food.fat / 100 * amount) const fat = food.fat / 100 * amount;
const carbs = Math.round(food.carbs / 100 * amount); const carbs = food.carbs / 100 * amount;
const sugar = Math.round(food.sugar / 100 * amount); const sugar = food.sugar / 100 * amount;
const proteins = Math.round(food.proteins / 100 * amount); const proteins = food.proteins / 100 * amount;
const alc = Math.round(amount * food.alc / 100); const alc = amount * food.alc / 100;
const kcal = Math.round(fat * 9 + carbs * 4 + proteins * 4 + alc * 7); const kcal = fat * 9 + carbs * 4 + proteins * 4 + alc * 7;
const score = kcal / amount; const score = kcal / amount;
return {fat, carbs, sugar, proteins, alc, kcal, score} return {fat, carbs, sugar, proteins, alc, kcal, score}
} }
@ -196,20 +209,20 @@ function updateSummary() {
let alc = 0; let alc = 0;
for (let menuItem of menuItems) { for (let menuItem of menuItems) {
energy += parseInt(menuItem.querySelector(".menu-item-energy").textContent); energy += parseFloat(menuItem.querySelector(".menu-item-energy").getAttribute("data-value"));
fat += parseInt(menuItem.querySelector(".menu-item-fat").textContent); fat += parseFloat(menuItem.querySelector(".menu-item-fat").getAttribute("data-value"));
carbs += parseInt(menuItem.querySelector(".menu-item-carbs").textContent); carbs += parseFloat(menuItem.querySelector(".menu-item-carbs").getAttribute("data-value"));
sugar += parseInt(menuItem.querySelector(".menu-item-sugar").textContent); sugar += parseFloat(menuItem.querySelector(".menu-item-sugar").getAttribute("data-value"));
proteins += parseInt(menuItem.querySelector(".menu-item-proteins").textContent); proteins += parseFloat(menuItem.querySelector(".menu-item-proteins").getAttribute("data-value"));
alc += parseInt(menuItem.querySelector(".menu-item-alc").textContent); alc += parseFloat(menuItem.querySelector(".menu-item-alc").getAttribute("data-value"));
} }
summaryEnergy.textContent = `${energy}kcal`; summaryEnergy.textContent = `${formatValue(energy)}kcal`;
summaryFat.textContent = `${fat}g`; summaryFat.textContent = `${formatValue(fat)}g`;
summaryCarbs.textContent = `${carbs}g`; summaryCarbs.textContent = `${formatValue(carbs)}g`;
summarySugar.textContent = `${sugar}g`; summarySugar.textContent = `${formatValue(sugar)}g`;
summaryProteins.textContent = `${proteins}g`; summaryProteins.textContent = `${formatValue(proteins)}g`;
summaryAlc.textContent = `${alc}g`; summaryAlc.textContent = `${formatValue(alc)}g`;
} }
function getScoreColor(density, unit) { function getScoreColor(density, unit) {

View File

@ -136,10 +136,6 @@ tfoot tr td {
margin: 0 auto; margin: 0 auto;
} }
/* .menu-item:not(:last-child) {
border-bottom: 1px solid lightgray;
} */
.delete-menu-item, .delete-menu-item,
.summary-delete-icon { .summary-delete-icon {
color: #cc3d3d; color: #cc3d3d;