function sumArray (inputArray) {
let sum = 0;
for (let i = 0; i < inputArray.length; i++) {
sum = sum + inputArray[i]
}
return sum}
class Ingredient {
constructor (name, ingredientAmount, protein100g, carbs100g, fat100g, fiber100g, iron100mg, sodium100mg, calcium100mg, vitaminD100g, calories100g) {
this.name = name;
this.ingredientAmount = ingredientAmount;
//macronutrients
this.protein100g = protein100g;
this.calculatedProtein = undefined;
this.carbs100g = carbs100g;
this.calculatedCarbs = undefined;
this.fat100g = fat100g;
this.calculatedFat = undefined;
this.fiber100g = fiber100g;
this.calculatedFiber = undefined;
//micronutrients
this.iron100mg = iron100mg;
this.calculatedIron = undefined ;
this.sodium100mg = sodium100mg;
this.calculatedSodium = undefined;
this.calcium100mg = calcium100mg;
this.calculatedCalcium = undefined;
this.vitaminD100g = vitaminD100g;
this.calculatedVitaminD = undefined;
//calorie
this.calories100g = calories100g;
this.calculatedCalories = undefined;
}
}
let ingredients = [
// new Ingredient ('carrot', 15, 0.9, 9.6, 0.2, 2.8, 0.3, 69, 33, 0, 41),
// new Ingredient ('ham', 25, 21, 0.4, 15, 0, 1.4, 941, 8, 0, 226),
// new Ingredient ('peas', 10, 5.4, 15, 0.4, 5.7, 1.5, 5, 25, 0, 81),
//new Ingredient ('rice', 450, 2.7, 28, 0.3, 0.4, 1.2, 1, 10, 0, 130),
//new recipie
new Ingredient ('butter', 42, 0.9, 0.1, 81, 0, 0, 11, 24, 60, 716),
new Ingredient ('egg', 88, 13, 1.1, 11, 0, 1.2, 124, 50, 87, 155.1),
new Ingredient ('carrot', 120, 0.9, 10, 0.2, 2.8, 0.3, 69, 33, 0, 41.3) ,
new Ingredient ('onion', 125, 1.3, 10, 1.3, 0, 0, 0, 26.5, 0, 45),
new Ingredient ('peas', 70, 5, 14, 0.4, 5, 1.5, 5, 25, 0, 81),
new Ingredient ('garlic', 18, 1.14, 5.95, 0.09, 0.1, 0.31, 3, 33, 0, 27),
new Ingredient ('rice', 744, 2.7, 28, 0.3, 0.4, 0.2, 1, 10, 0, 130),
new Ingredient ('green onion', 36, 1.8, 7, 0.2, 2.6, 1.5, 16, 72, 0, 32.3),
new Ingredient ('soy sauce', 56, 8, 4.9, 0.6, 0.8, 1.5, 5493, 33, 0, 53.1),
new Ingredient ('oyster sauce', 12, 1.4, 11, 0.3, 0.3, 0.2, 2733, 32, 0, 50.9),
new Ingredient ('sesame oil', 2.25, 0, 0, 100, 0, 0, 0, 0, 0, 844.1),
]
function calculateNutrition (ingredientObj) {
ingredientObj.calculatedProtein = (ingredientObj.protein100g/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedCarbs = (ingredientObj.carbs100g/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedFat = (ingredientObj.fat100g/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedFiber = (ingredientObj.fiber100g/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedIron = (ingredientObj.iron100mg/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedSodium = (ingredientObj.sodium100mg/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedCalcium = (ingredientObj.calcium100mg/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedVitaminD = (ingredientObj.vitaminD100g/100) * ingredientObj.ingredientAmount / 5;
ingredientObj.calculatedCalories = (ingredientObj.calories100g/100) * ingredientObj.ingredientAmount / 5;
return ingredientObj
}
function nutritionPrinter (inputArray, totalAmount, ingredients, 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.ingredientAmount + 'g');
console.log("the amount of protein",i.protein100g + 'g');
console.log("the amount of carbs", i.carbs100g + 'g');
console.log("the amount of fat",i.fat100g + 'g');
console.log("the amount of fiber",i.fiber100g + 'g');
console.log("the amount of iron",i.iron100mg + 'mg');
console.log("the amount of sodium",i.sodium100mg + 'mg');
console.log("the amount of calcium",i.calcium100mg + 'mg');
console.log("the amount of vitaminD",i.vitaminD100g + 'g');
console.log("the amount of calories", i.calories100g + 'calories')})
}
function totalNutrition (ingredientArray) {
ingredientArray = ingredientArray.map(calculateNutrition)
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 allWeightValues = ingredientArray.map(o => o.ingredientAmount/5)
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 totalCalories = sumArray (allCaloriesValues)
let totalWeight = sumArray (allWeightValues)
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,
weight: totalWeight
}
console.log('the name of this dish is ', nutritionObj.name);
console.log('the total weight of this dish is', nutritionObj.weight + 'g');
console.log('the total amount of protein is ', nutritionObj.protein + 'g');
console.log('the total amount of carb is ', nutritionObj.carb + 'g');
console.log('the total amount of fat is ', nutritionObj.fat + 'g');
console.log('the total amount of fiber is ', nutritionObj.fiber + 'g');
console.log('the total amount of iron is ', nutritionObj.iron + 'mg');
console.log('the total amount of sodium is ', nutritionObj.sodium + 'mg');
console.log('the total amount of calcium is ', nutritionObj.calcium + 'mg');
console.log('the total amount of vitaminD is ', nutritionObj.vitaminD + 'g');
console.log('the totalAmount of calories is', nutritionObj.calories + 'calories');
return nutritionObj
}
totalNutrition(ingredients)