function calculateProtein (ingredientWeight, ingredientPercentage, proteinIngredient100g, ingredientWeightProportion, ingredientTotalProtein) { let friedRiceWeight = 450 let carrotWeight = 15 let hamWeight = 25 let peasWeight = 10 let whiteRiceWeight = 450 let proteinCarrot100g = 0.9 let proteinHam100g = 21 let proteinPeas100g = 5.4 let proteinWhiteRice100g = 2.7 let totalProtein = ingredientWeightProportion+ ingredientTotalProtein; return totalProtein } class Ingredient { constructor (name, weight, protein100g, carbs100g, fat100g, fiber100g, iron100g, sodium100g, calcium100g, vitaminD100g,calories100g) { this.name = name; this.weight = weight; this.protein100g = protein100g; this.calculatedProtein = undefined; this.carbs100g = carbs100g; this.calculatedCarbs = undefined; this.fat100g = fat100g; this.calculatedfat = undefined; this.fiber100g = fiber100g; this.calculatedfiber = undefined; this.iron100g = iron100g; this.calculatediron = undefined; this.sodium100g = sodium100g; this.calculatedsodium = undefined; this.calcium100g = calcium100g; this.calculatedcalcium = undefined; this.vitaminD100g = vitaminD100g; this.calculatedvitaminD = undefined; this.calories100g = calories100g; this.calculatedCalories = undefined; } } function calculateNutrion (ingredientObject) { ingredientObject.calculatedProtein = ingredientObject.protein100g / 100 * ingredientObject.weight; ingredientObject.calculatedCarbs = ingredientObject.carbs100g / 100 * ingredientObject.weight; ingredientObject.calculatedfat = ingredientObject.fat100g / 100 * ingredientObject.weight; ingredientObject.calculatedfiber = ingredientObject.fiber100g / 100 * ingredientObject.weight; ingredientObject.calculatediron = ingredientObject.iron100g / 100 * ingredientObject.weight; ingredientObject.calculatedsodium = ingredientObject.sodium100g / 100 * ingredientObject.weight; ingredientObject.calculatedcalcium = ingredientObject.calcium100g / 100 * ingredientObject.weight; ingredientObject.calculatedvitaminD = ingredientObject.vitaminD100g / 100 * ingredientObject.weight; ingredientObject.calculatedCalories = ingredientObject.calories100g / 100 * ingredientObject.weight; return ingredientObject } let allIngredient = [ new Ingredient('carrot', 15, 0.9, 9.6, 0.2, 2.8, 0.0003, 0.069, 0.033, 0, 41), new Ingredient('rice', 450, 2.7, 28, 2.6, 0.7, 0.0012, 0.001, 0.01, 0, 130), new Ingredient('peas', 10, 5.4, 15, 0.4, 5.7, 0.0015, 0.005, 0.025, 0, 81), new Ingredient('ham', 25, 21, 0.4, 15, 0, 0.0014, 0.941, 0.008, 0, 226), ] function nutritionPrinter (inputArray, weight, allingredients, totalProtein, totalCarb, totalFat, totalFiber, totalIron, totalSodium, totalCalcium, totalVitaminD, totalCalories) { inputArray.forEach(i => { console.log("the ingredient name is",i.name); console.log("the ingredient amount is ",i.weight); console.log("the amount of protein",i.protein100g); console.log("the amount of carbs", i.carbs100g); console.log("the amount of fat",i.fat100g); console.log("the amount of fiber",i.fiber100g); console.log("the amount of iron",i.iron100g); console.log("the amount of sodium",i.sodium100g); console.log("the amount of calcium",i.calcium100g); console.log("the amount of vitaminD",i.vitaminD100g)}) console.log("the amount of Calories",i.calories100g) } function totalNutrition (ingredientArray) { ingredientArray = ingredientArray.map(calculateNutrion) let allProteinValues = ingredientArray.map(o => o.calculatedProtein) let allCarbValues = ingredientArray.map(o => o.calculatedCarbs) let allFatValues = ingredientArray.map(o => o.calculatedfat) let allFiberValues = ingredientArray.map(o => o.calculatedfiber) let allIronValues = ingredientArray.map(o => o.calculatediron) let allSodiumValues = ingredientArray.map(o => o.calculatedsodium) let allCalciumValues = ingredientArray.map(o => o.calculatedcalcium) let allVitaminDValues = ingredientArray.map(o => o.calculatedvitaminD) let allCaloriesValues = ingredientArray.map(o => o.calculatedCalories) let totalProtein = sumArray (allProteinValues) let totalCarb = sumArray (allCarbValues) let totalFat = sumArray (allFatValues) let totalFiber = sumArray (allFiberValues) let totalIron = sumArray (allIronValues) let totalSodium = sumArray (allSodiumValues) let totalCalcium = sumArray (allCalciumValues) let totalVitaminD = sumArray (allVitaminDValues) let totalCalores = sumArray (allCaloriesValues) nutritionPrinter (ingredientArray, totalProtein, totalCarb, totalFat, totalFiber, totalIron, totalSodium, totalCalcium, totalVitaminD, totalCalories) let nutritionObj = { name:'fried rice', protein: totalProtein, carb: totalCarb, fat: totalFat, fiber: totalFiber, iron: totalIron, sodium: totalSodium, calcium: totalCalcium, vitaminD: totalVitaminD, Calories: totalCalories, } console.log('the name of this dish is ', nutritionObj.name); console.log('the total amount of protein is ', nutritionObj.protein); console.log('the total amount of carb is ', nutritionObj.carb); console.log('the total amount of fat is ', nutritionObj.fat); console.log('the total amount of fiber is ', nutritionObj.fiber); console.log('the total amount of iron is ', nutritionObj.iron); console.log('the total amount of sodium is ', nutritionObj.sodium); console.log('the total amount of calcium is ', nutritionObj.calcium); console.log('the total amount of vitaminD is ', nutritionObj.vitaminD); console.log('the total amount of calories is ', nutritionObj.calories); return nutritionObj } calculateProtein(allIngredient[0]) calculateProtein(allIngredient[1]) calculateProtein(allIngredient[2]) calculateProtein(allIngredient[3]) allIngredient2 = allIngredient.map(calculateNutrion) totalNutrition(allIngredient)