====== Nutrition Calculator ====== class Ingredient { constructor (name, amountInDish, proteinAmount100g, carbsAmount100g, fiberAmount100g, fatAmount100g, ironAmount100mg, sodiumAmount100mg, calciumAmount100mg, vitaminDAmount100µg, caloriesAmount100g){ this.name = name; this.amountInDish = amountInDish; this.proteinAmount100g = proteinAmount100g; this.carbsAmount100g = carbsAmount100g; this.fiberAmount100g = fiberAmount100g; this.fatAmount100g = fatAmount100g; this.ironAmount100mg = ironAmount100mg; this.sodiumAmount100mg = sodiumAmount100mg; this.calciumAmount100mg = calciumAmount100mg; this.vitaminDAmount100µg = vitaminDAmount100µg; this.caloriesAmount100g = caloriesAmount100g; this.calculatedCarbs=undefined; this.calculatedProtein=undefined; this.calculatedFiber=undefined; this.calculatedFat=undefined; this.calculatedIron=undefined; this.calculatedSodium=undefined; this.calculatedCalcium=undefined; this.calculatedVitaminD=undefined; this.calculatedCalories=undefined; } } function calculatedNutrition (ingredientObject) { ingredientObject.calculatedProtein = ingredientObject.proteinAmount100g / 100 * ingredientObject.amountInDish; ingredientObject.calculatedCarbs = ingredientObject.carbsAmount100g /100 * ingredientObject.amountInDish; ingredientObject.calculatedFiber = ingredientObject.fiberAmount100g /100 * ingredientObject.amountInDish; ingredientObject.calculatedFat = ingredientObject.fatAmount100g /100 * ingredientObject.amountInDish; ingredientObject.calculatedIron = ingredientObject.ironAmount100mg /100 * ingredientObject.amountInDish; ingredientObject.calculatedSodium = ingredientObject.sodiumAmount100mg /100 * ingredientObject.amountInDish; ingredientObject.calculatedCalcium = ingredientObject.calciumAmount100mg /100 * ingredientObject.amountInDish; ingredientObject.calculatedVitaminD = ingredientObject.vitaminDAmount100µg /100 * ingredientObject.amountInDish; ingredientObject.calculatedCalories = ingredientObject.caloriesAmount100g /100 * ingredientObject.amountInDish; return ingredientObject } let ingredients = [ //new Ingredient ('carrot',100,0.9,9.6,2.8,0.2,0.3,69,33,0,41), // new Ingredient ('ham',100,29,0.6,0,21,1.9,1317,11.2,0,316), // new Ingredient ('peas',100,7.9,21,8.3,0.6,2.1,7.3,36.3,0,117), //new Ingredient ('rice',100,2.7,28,0.4,0.3,1.2,1,10,0,130), //new Ingredient ('vegetable oil',20,0,0,0,20,0,0,0,0,177), //new Ingredient ('soy sauce',4,0,4.9,0,0.6,0.1,219.7,1.3,0,2), //new Ingredient ('fried eggs',56,6.3,0.4,0,6.8,0.9,95.2,29,1,90), //new Ingredient ('onion',50,0.6,5,0.8,0,0.1,2,11.5,0,20), new Ingredient ('butter',42,0.08,0,0,6.49,0,1,2,0,61), new Ingredient ('eggs',88,2.85,0.65,0,4.78,0.49,9,23,0,58), new Ingredient ('carrots',120,0.22,2.3,0.7,0.06,0.07,17,8,0,20), new Ingredient ('small white onion',125,0.23,2.53,0.4,0.02,0.05,1,6,0,10), new Ingredient ('frozen peas',70,0.76,2.02,0.7,0.06,0.21,1,4,0,11), new Ingredient ('garlic cloves',18,0.25,1.32,0.1,0.02,0.07,1,7,0,6), new Ingredient ('medium white rice',744,3.99,41.85,0.6,0.42,1.78,548,15,0,194), new Ingredient ('green onions',36,0.13,0.51,0.2,0.01,0.1,1,5,0,2), new Ingredient ('soy sauce',56,0.69,0.84,0.1,0,0.21,620,2,0,6), new Ingredient ('oyster sauce',12,5.5,7.8,8.1,0.2,3.6,242,79,0.6,127,5), new Ingredient ('sesame oil',2.25,0,0,100,0,0,0,0,0,884,5), new Ingredient ('frozen blueberries',300,2.26,43.47,7.2,0.99,0.84,3,18,0,171), ] ingredients = ingredients.map(calculatedNutrition) function sumArray (inputArray) { let sum = 0; for (let i = 0; i < inputArray.length; i++) { sum = sum + inputArray[i] } return sum} let allAmountInDishValues = ingredients.map(ingredientObject => ingredientObject.amountInDish) let allProteinValues = ingredients.map(ingredientObject => ingredientObject.calculatedProtein) let allCarbValues = ingredients.map(ingredientObject => ingredientObject.calculatedCarbs) let allFiberValues = ingredients.map(ingredientObject => ingredientObject.calculatedFiber) let allFatValues = ingredients.map(ingredientObject => ingredientObject.calculatedFat) let allIronValues = ingredients.map(ingredientObject => ingredientObject.calculatedIron) let allSodiumValues = ingredients.map(ingredientObject => ingredientObject.calculatedSodium) let allCalciumValues = ingredients.map(ingredientObject => ingredientObject.calculatedCalcium) let allVitaminDValues = ingredients.map(ingredientObject => ingredientObject.calculatedVitaminD) let allCaloriesValues = ingredients.map(ingredientObject => ingredientObject.calculatedCalories) let totalIngredientWeight = sumArray (allAmountInDishValues) / 5; let totalProtein = sumArray (allProteinValues) / 5; let totalCarbs = sumArray (allCarbValues) / 5; let totalFiber = sumArray (allFiberValues) / 5; let totalFat = sumArray (allFatValues) / 5; let totalIron = sumArray (allIronValues) / 5; let totalSodium = sumArray (allSodiumValues) / 5; let totalCalcium = sumArray (allCalciumValues) / 5; let totalVitaminD = sumArray (allVitaminDValues) / 5; let totalCalories = sumArray (allCaloriesValues) / 5; function printCalculatedNutrition (inputArray, totalAmountInDish, totalAmountProtein, totalAmountCarbs, totalAmountFiber, totalAmountFat, totalAmountIron, totalAmountSodium, totalAmountCalcium, totalAmountVitaminD, totalAmountCalories) { inputArray.forEach(ingredientObject => {console.log("nutrition data"); console.log("ingredient weight is:",ingredientObject.amountInDish); console.log("protein Amount in 100g is:", ingredientObject.proteinAmount100g,"g"); console.log("calculatedProtein is:", ingredientObject.calculatedProtein,"g"); console.log("amount of carbs in 100g is:", ingredientObject.carbsAmount100g,"g"); console.log("amount carbs in fried rice is:", ingredientObject.calculatedCarbs,"g"); console.log("amount of fiber in 100g is:", ingredientObject.fiberAmount100g,"g"); console.log("amount fiber in fried rice is:", ingredientObject.calculatedFiber,"g"); console.log("amount of fat in 100g is:", ingredientObject.fatAmount100g,"g"); console.log("amount fat in fried rice is:", ingredientObject.calculatedFat,"g"); console.log("amount of iron in 100mg is:", ingredientObject.ironAmount100mg,"mg"); console.log("amount iron in fried rice is:", ingredientObject.calculatedIron,"mg"); console.log("amount of sodium in 100mg is:", ingredientObject.sodiumAmount100mg,"mg"); console.log("amount sodium in fried rice is:", ingredientObject.calculatedSodium,"mg"); console.log("amount of calcium in 100mg is:",ingredientObject.calciumAmount100mg,"mg"); console.log("amount calcium in fried rice is", ingredientObject.calculatedCalcium,"mg"); console.log("amount of vitamin D in 100µg is",ingredientObject.vitaminDAmount100µg,"µg"); console.log("amount vitamin D in fried rice is", ingredientObject.calculatedVitaminD,"µg"); console.log("amount of calories in 100g is:",ingredientObject.caloriesAmount100g,"kcal"); console.log("amount of calories in fried rice is:", ingredientObject.calculatedCalories,"kcal"); }); console.log("these are my totals for fried rice:"); console.log("total weight for fried rice ingredients" ,totalAmountInDish,"g") console.log("total amount of fried rice protein:" ,totalAmountProtein,"g"); console.log("total amount of fried rice carbs:" ,totalAmountCarbs,"g"); console.log("total amount of fried rice fiber:" ,totalAmountFiber,"g"); console.log("total amount of fried rice fat:" ,totalAmountFat,"g"); console.log("total amount of fried rice Iron:" ,totalAmountIron,"mg"); console.log("total amount of fried rice sodium:" ,totalAmountSodium,"mg"); console.log("total amount of fried rice calcium:" ,totalAmountCalcium,"mg"); console.log("total amount of fried rice vitamin D:" ,totalAmountVitaminD,"µg"); console.log("total amount of calories in fried rice is:" ,totalAmountCalories,"kcal"); } printCalculatedNutrition(ingredients,totalIngredientWeight,totalProtein,totalCarbs,totalFat,totalFiber,totalIron,totalSodium,totalCalcium,totalVitaminD,totalCalories) these are my totals for fried rice: VM584:142 total weight for fried rice ingredients 322.65 g VM584:143 total amount of fried rice protein: 8.24578 g VM584:144 total amount of fried rice carbs: 90.30202 g VM584:145 total amount of fried rice fiber: 2.6394399999999996 g VM584:146 total amount of fried rice fat: 6.2524 g VM584:147 total amount of fried rice Iron: 3.4172199999999995 mg VM584:148 total amount of fried rice sodium: 898.7180000000001 mg VM584:149 total amount of fried rice calcium: 44.047999999999995 mg VM584:150 total amount of fried rice vitamin D: 0.014400000000000001 µg VM584:151 total amount of calories in fried rice is: 423.50200000000007 kcal