Ingredient Grams kJ Sugars Satur. fat Salt Fruit Fibre Protein
{ingredient.score}

Nutriscore: {nutriscore}
this.nutriscore = "?" this.default = { "amount": 100, "kj": 500, "sugars": 1, "sat_fat": 10, "salt": 0.1, "fibre": 0, "fruit": 0, "protein": 30, } this.kcal2kj = 4.148 this.ingredients = [Object.assign({}, this.default)] this.food = [ // name kcal protein carbs sugar fat satfat fibre salt ["kokos", 185, 2, 2, 2, 19, 17, 0, 0.05, 0], ["zazvor", 80, 2, 17, 2, 0.75, 0.2, 2, 0.13, 0], ["rajcata", 26, 1.3, 4, 2.7, 0.2, 0.1, 0, 0.02, 100], ["brokolice", 28, 2, 6, 2, 0.2, 0, 3, 0, 100], ["sojovka", 56, 8, 6, 0, 0.4, 0, 0, 13, 0], ["čočka", 127, 8, 19, 0.43, 0.43, 0.07, 4, 0, 0], ["rýže", 121, 2, 26, 0, 0.08, 0, 0.93, 0, 0], ["vločky", 354, 12, 61, 1, 5, 0.8, 9, 0.01, 0], ["chia", 495, 21, 20, 4, 31, 3, 41, 0.01, 0], ["kešu máslo", 553, 18, 33, 6, 44, 8, 3, 0], ["rybíz", 89, 1, 17, 8, 0.27, 0.03, 7, 0, 100], ["borůvky", 57, 0.74, 12, 10, 0.33, 0, 2, 0, 100], ] select_change(event) { let f = this.food[event.target.selectedIndex-1] let x = this.ingredients[event.item.index] x.kj = f[1] * Math.ceil(this.kcal2kj) x.sugars = f[4] x.sat_fat = f[6] x.salt = f[8] x.fibre = f[7] x.fruit = f[9] x.protein = f[2] x.score = this.compute_one(x) } update_value(event) { this.ingredients[event.item.index][event.target.name] = event.target.value this.ingredients[event.item.index].score = this.compute_one( this.ingredients[event.item.index] ) } remove_ingredient(index, event) { event.preventDefault() this.ingredients.splice(index, 1) } add_ingredient(event) { event.preventDefault() this.ingredients.push(Object.assign({}, this.default)) } a_a(kj) { if (kj > 3350) return 10 if (kj > 3015) return 9 if (kj > 2680) return 8 if (kj > 2345) return 7 if (kj > 2010) return 6 if (kj > 1675) return 5 if (kj > 1340) return 4 if (kj > 1005) return 3 if (kj > 670) return 2 if (kj > 335) return 1 return 0 } a_b(sugars) { if (sugars > 45) return 10 if (sugars > 40) return 9 if (sugars > 36) return 8 if (sugars > 31) return 7 if (sugars > 27) return 6 if (sugars > 22.5) return 5 if (sugars > 18) return 4 if (sugars > 13.5) return 3 if (sugars > 9) return 2 if (sugars > 4.5) return 1 return 0 } a_c(sat_fat) { return Math.floor(Math.min(sat_fat, 10.1)) } a_d(salt) { let na = salt * 0.4 if (na > 0.900) return 10 if (na > 0.810) return 9 if (na > 0.720) return 8 if (na > 0.630) return 7 if (na > 0.540) return 6 if (na > 0.450) return 5 if (na > 0.360) return 4 if (na > 0.270) return 3 if (na > 0.180) return 2 if (na > 0.90) return 1 return 0 } c_a(fruit) { // percentage! if (fruit > 80) return 5 if (fruit > 60) return 2 if (fruit > 40) return 1 return 0 } c_b(fibre) { if (fibre > 4.7) return 5 if (fibre > 3.7) return 4 if (fibre > 2.8) return 3 if (fibre > 1.9) return 2 if (fibre > 0.9) return 1 return 0 } c_c(protein) { if (protein > 8.0) return 5 if (protein > 6.4) return 4 if (protein > 4.8) return 3 if (protein > 3.2) return 2 if (protein > 1.6) return 1 return 0 } get_nutriscore(kj, sugars, sat_fat, salt, fruit, fibre, protein) { let points_a = this.a_a(kj) + this.a_b(sugars) + this.a_c(sat_fat) + this.a_d(salt) let points_c = this.c_a(fruit) + this.c_b(fibre) + this.c_c(protein) let sum = points_a - points_c if (sum <= -1) return "A" if (sum >= 19) return "E" if (sum >= 11) return "D" if (sum >= 3) return "C" if (sum >= 0) return "B" } compute_one(i) { return this.get_nutriscore( i.kj, i.sugars, i.sat_fat, i.salt, i.fruit, i.fibre, i.protein ) } compute(event) { let amount = 0 let kj = 0 let sugars = 0 let sat_fat = 0 let salt = 0 let fruit = 0 let fibre = 0 let protein = 0 for (let i=0; i