Nutrition Calculator

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)

outputs

the ingredient name is butter
VM327:74 the ingredient amount is  42g
VM327:75 the amount of protein 0.9g
VM327:76 the amount of carbs 0.1g
VM327:77 the amount of fat 81g
VM327:78 the amount of fiber 0g
VM327:79 the amount of iron 0mg
VM327:80 the amount of sodium 11mg
VM327:81 the amount of calcium 24mg
VM327:82 the amount of vitaminD 60g
VM327:83 the amount of calories 716calories
VM327:73 the ingredient name is egg
VM327:74 the ingredient amount is  88g
VM327:75 the amount of protein 13g
VM327:76 the amount of carbs 1.1g
VM327:77 the amount of fat 11g
VM327:78 the amount of fiber 0g
VM327:79 the amount of iron 1.2mg
VM327:80 the amount of sodium 124mg
VM327:81 the amount of calcium 50mg
VM327:82 the amount of vitaminD 87g
VM327:83 the amount of calories 155.1calories
VM327:73 the ingredient name is carrot
VM327:74 the ingredient amount is  120g
VM327:75 the amount of protein 0.9g
VM327:76 the amount of carbs 10g
VM327:77 the amount of fat 0.2g
VM327:78 the amount of fiber 2.8g
VM327:79 the amount of iron 0.3mg
VM327:80 the amount of sodium 69mg
VM327:81 the amount of calcium 33mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 41.3calories
VM327:73 the ingredient name is onion
VM327:74 the ingredient amount is  125g
VM327:75 the amount of protein 1.3g
VM327:76 the amount of carbs 10g
VM327:77 the amount of fat 1.3g
VM327:78 the amount of fiber 0g
VM327:79 the amount of iron 0mg
VM327:80 the amount of sodium 0mg
VM327:81 the amount of calcium 26.5mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 45calories
VM327:73 the ingredient name is peas
VM327:74 the ingredient amount is  70g
VM327:75 the amount of protein 5g
VM327:76 the amount of carbs 14g
VM327:77 the amount of fat 0.4g
VM327:78 the amount of fiber 5g
VM327:79 the amount of iron 1.5mg
VM327:80 the amount of sodium 5mg
VM327:81 the amount of calcium 25mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 81calories
VM327:73 the ingredient name is garlic
VM327:74 the ingredient amount is  18g
VM327:75 the amount of protein 1.14g
VM327:76 the amount of carbs 5.95g
VM327:77 the amount of fat 0.09g
VM327:78 the amount of fiber 0.1g
VM327:79 the amount of iron 0.31mg
VM327:80 the amount of sodium 3mg
VM327:81 the amount of calcium 33mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 27calories
VM327:73 the ingredient name is rice
VM327:74 the ingredient amount is  744g
VM327:75 the amount of protein 2.7g
VM327:76 the amount of carbs 28g
VM327:77 the amount of fat 0.3g
VM327:78 the amount of fiber 0.4g
VM327:79 the amount of iron 0.2mg
VM327:80 the amount of sodium 1mg
VM327:81 the amount of calcium 10mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 130calories
VM327:73 the ingredient name is green onion
VM327:74 the ingredient amount is  36g
VM327:75 the amount of protein 1.8g
VM327:76 the amount of carbs 7g
VM327:77 the amount of fat 0.2g
VM327:78 the amount of fiber 2.6g
VM327:79 the amount of iron 1.5mg
VM327:80 the amount of sodium 16mg
VM327:81 the amount of calcium 72mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 32.3calories
VM327:73 the ingredient name is soy sauce
VM327:74 the ingredient amount is  56g
VM327:75 the amount of protein 8g
VM327:76 the amount of carbs 4.9g
VM327:77 the amount of fat 0.6g
VM327:78 the amount of fiber 0.8g
VM327:79 the amount of iron 1.5mg
VM327:80 the amount of sodium 5493mg
VM327:81 the amount of calcium 33mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 53.1calories
VM327:73 the ingredient name is oyster sauce
VM327:74 the ingredient amount is  12g
VM327:75 the amount of protein 1.4g
VM327:76 the amount of carbs 11g
VM327:77 the amount of fat 0.3g
VM327:78 the amount of fiber 0.3g
VM327:79 the amount of iron 0.2mg
VM327:80 the amount of sodium 2733mg
VM327:81 the amount of calcium 32mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 50.9calories
VM327:73 the ingredient name is sesame oil
VM327:74 the ingredient amount is  2.25g
VM327:75 the amount of protein 0g
VM327:76 the amount of carbs 0g
VM327:77 the amount of fat 100g
VM327:78 the amount of fiber 0g
VM327:79 the amount of iron 0mg
VM327:80 the amount of sodium 0mg
VM327:81 the amount of calcium 0mg
VM327:82 the amount of vitaminD 0g
VM327:83 the amount of calories 844.1calories
VM327:130 the name of this dish is  fried rice
VM327:131 the total weight of this dish is 262.65g
VM327:132 the total amount of protein is  8.72244g
VM327:133 the total amount of carb is  50.257000000000005g
VM327:134 the total amount of fat is  10.157439999999998g
VM327:135 the total amount of fiber is  2.2548g
VM327:136 the total amount of iron is  1.08276mg
VM327:137 the total amount of sodium is  723.564mg
VM327:138 the total amount of calcium is  54.577mg
VM327:139 the total amount of vitaminD is  20.352g
VM327:140 the totalAmount of calories is 327.64845calories