lucas-tam-nutrition-calculator

Lucas Tam's Nutrition Calculator

Can you help your health with code? For the last month or so, we’ve been working on a project called a nutrition calculator. This code is supposed to calculate all the different ingredients in our fried rice. This is important because this is for nutrition in space. In this essay, I will talk about my program, how I fixed it, and what I learned.

My code is basically about the amount of nutrition in each ingredient in my fried rice. For example, in my fried rice I have eggs, so then I will go on to Google and search the nutrition data for eggs 100g. After, I put the data into my code and I run it to see if I got an answer. I had to do this for all ingredients in my fried rice. I also used a class to organize my ingredients, a function to calculate all the nutrition, and the printer to print out human readable information.

To be honest, the fried rice calculator was one of the hardest code I've ever done. There were many problems that I had to fix. I did that by asking my mom for some help and looking at others code from the wiki. Some people may believe that this is copying but since I was in a roadblock with my code, I could look at someone elses code to understand what they did and put it in my code. Also, if it still ends up not running, you need to write a comment saying that your code is broken because if you don't it's will be a waste of the teacher's time and your time.

Lastly, I learned that I was quite irresponsible and maybe a bit lazy when doing my work. But I also learned that everything we do is for a reason, no matter how much I disliked doing the fried rice calculator I knew it was still very important.

All in all, talking about my progam, how I fixed it, and what I learned helped me communicate with the public. The first time I was told to do this I had no clue about what I was doing. Now, I understand the code a bit more and learned a couple of intresting stuff. Now, you should try to make your own fried rice using any ingredient. If your fried rice is healthy enough, you can try and code it too.

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 ('frozen blueberries',300 ,2.26, 43.47, 7.2, 0.99, 0.84, 3, 18, 0, 50 ),
new Ingredient ('Honey Yogurt', 350, 3.6, 12.8, 5, 0, 57, 106, 0, 0, 113 ),
new Ingredient ('milk', 150, 3.2, 4.8, 3.3, 0, 43, 113, 0, 1.3, 61),
new Ingredient ('ice', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
//recipe2
new Ingredient ('milk', 150, 3.2, 4.8, 3.3, 0, 43, 113, 0, 1.3, 61),
new Ingredient ('watermelon', 200, 0.6, 7.6, 0.2, 0.4, 1, 7, 0.2, 0, 30),
new Ingredient ('pineapple',300, 0.5, 13, 0.1, 1.4, 0.3, 1, 13, 0, 50 ),
]
  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 smoothie:");
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)
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 300
VM3003:102 protein Amount in 100g is: 2.26 g
VM3003:103 calculatedProtein is: 6.779999999999999 g
VM3003:105 amount of carbs in 100g is: 43.47 g
VM3003:106 amount carbs in fried rice is: 130.41 g
VM3003:108 amount of fiber in 100g is: 7.2 g
VM3003:109 amount fiber in fried rice is: 21.6 g
VM3003:111 amount of fat in 100g is: 0.99 g
VM3003:112 amount fat in fried rice is: 2.9699999999999998 g
VM3003:114 amount of iron in 100mg is: 0.84 mg
VM3003:115 amount iron in fried rice is: 2.52 mg
VM3003:117 amount of sodium in 100mg is: 3 mg
VM3003:118 amount sodium in fried rice is: 9 mg
VM3003:120 amount of calcium in 100mg is: 18 mg
VM3003:121 amount calcium in fried rice is 54 mg
VM3003:123 amount of vitamin D in 100µg is 0 µg
VM3003:124 amount vitamin D in fried rice is 0 µg
VM3003:126 amount of calories in 100g is: 50 kcal
VM3003:127 amount of calories in fried rice is: 150 kcal
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 350
VM3003:102 protein Amount in 100g is: 3.6 g
VM3003:103 calculatedProtein is: 12.600000000000001 g
VM3003:105 amount of carbs in 100g is: 12.8 g
VM3003:106 amount carbs in fried rice is: 44.800000000000004 g
VM3003:108 amount of fiber in 100g is: 5 g
VM3003:109 amount fiber in fried rice is: 17.5 g
VM3003:111 amount of fat in 100g is: 0 g
VM3003:112 amount fat in fried rice is: 0 g
VM3003:114 amount of iron in 100mg is: 57 mg
VM3003:115 amount iron in fried rice is: 199.49999999999997 mg
VM3003:117 amount of sodium in 100mg is: 106 mg
VM3003:118 amount sodium in fried rice is: 371 mg
VM3003:120 amount of calcium in 100mg is: 0 mg
VM3003:121 amount calcium in fried rice is 0 mg
VM3003:123 amount of vitamin D in 100µg is 0 µg
VM3003:124 amount vitamin D in fried rice is 0 µg
VM3003:126 amount of calories in 100g is: 113 kcal
VM3003:127 amount of calories in fried rice is: 395.49999999999994 kcal
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 150
VM3003:102 protein Amount in 100g is: 3.2 g
VM3003:103 calculatedProtein is: 4.8 g
VM3003:105 amount of carbs in 100g is: 4.8 g
VM3003:106 amount carbs in fried rice is: 7.2 g
VM3003:108 amount of fiber in 100g is: 3.3 g
VM3003:109 amount fiber in fried rice is: 4.95 g
VM3003:111 amount of fat in 100g is: 0 g
VM3003:112 amount fat in fried rice is: 0 g
VM3003:114 amount of iron in 100mg is: 43 mg
VM3003:115 amount iron in fried rice is: 64.5 mg
VM3003:117 amount of sodium in 100mg is: 113 mg
VM3003:118 amount sodium in fried rice is: 169.49999999999997 mg
VM3003:120 amount of calcium in 100mg is: 0 mg
VM3003:121 amount calcium in fried rice is 0 mg
VM3003:123 amount of vitamin D in 100µg is 1.3 µg
VM3003:124 amount vitamin D in fried rice is 1.9500000000000002 µg
VM3003:126 amount of calories in 100g is: 61 kcal
VM3003:127 amount of calories in fried rice is: 91.5 kcal
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 0
VM3003:102 protein Amount in 100g is: 0 g
VM3003:103 calculatedProtein is: 0 g
VM3003:105 amount of carbs in 100g is: 0 g
VM3003:106 amount carbs in fried rice is: 0 g
VM3003:108 amount of fiber in 100g is: 0 g
VM3003:109 amount fiber in fried rice is: 0 g
VM3003:111 amount of fat in 100g is: 0 g
VM3003:112 amount fat in fried rice is: 0 g
VM3003:114 amount of iron in 100mg is: 0 mg
VM3003:115 amount iron in fried rice is: 0 mg
VM3003:117 amount of sodium in 100mg is: 0 mg
VM3003:118 amount sodium in fried rice is: 0 mg
VM3003:120 amount of calcium in 100mg is: 0 mg
VM3003:121 amount calcium in fried rice is 0 mg
VM3003:123 amount of vitamin D in 100µg is 0 µg
VM3003:124 amount vitamin D in fried rice is 0 µg
VM3003:126 amount of calories in 100g is: 0 kcal
VM3003:127 amount of calories in fried rice is: 0 kcal
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 150
VM3003:102 protein Amount in 100g is: 3.2 g
VM3003:103 calculatedProtein is: 4.8 g
VM3003:105 amount of carbs in 100g is: 4.8 g
VM3003:106 amount carbs in fried rice is: 7.2 g
VM3003:108 amount of fiber in 100g is: 3.3 g
VM3003:109 amount fiber in fried rice is: 4.95 g
VM3003:111 amount of fat in 100g is: 0 g
VM3003:112 amount fat in fried rice is: 0 g
VM3003:114 amount of iron in 100mg is: 43 mg
VM3003:115 amount iron in fried rice is: 64.5 mg
VM3003:117 amount of sodium in 100mg is: 113 mg
VM3003:118 amount sodium in fried rice is: 169.49999999999997 mg
VM3003:120 amount of calcium in 100mg is: 0 mg
VM3003:121 amount calcium in fried rice is 0 mg
VM3003:123 amount of vitamin D in 100µg is 1.3 µg
VM3003:124 amount vitamin D in fried rice is 1.9500000000000002 µg
VM3003:126 amount of calories in 100g is: 61 kcal
VM3003:127 amount of calories in fried rice is: 91.5 kcal
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 200
VM3003:102 protein Amount in 100g is: 0.6 g
VM3003:103 calculatedProtein is: 1.2 g
VM3003:105 amount of carbs in 100g is: 7.6 g
VM3003:106 amount carbs in fried rice is: 15.2 g
VM3003:108 amount of fiber in 100g is: 0.2 g
VM3003:109 amount fiber in fried rice is: 0.4 g
VM3003:111 amount of fat in 100g is: 0.4 g
VM3003:112 amount fat in fried rice is: 0.8 g
VM3003:114 amount of iron in 100mg is: 1 mg
VM3003:115 amount iron in fried rice is: 2 mg
VM3003:117 amount of sodium in 100mg is: 7 mg
VM3003:118 amount sodium in fried rice is: 14.000000000000002 mg
VM3003:120 amount of calcium in 100mg is: 0.2 mg
VM3003:121 amount calcium in fried rice is 0.4 mg
VM3003:123 amount of vitamin D in 100µg is 0 µg
VM3003:124 amount vitamin D in fried rice is 0 µg
VM3003:126 amount of calories in 100g is: 30 kcal
VM3003:127 amount of calories in fried rice is: 60 kcal
VM3003:100 nutrition data
VM3003:101 ingredient weight is: 300
VM3003:102 protein Amount in 100g is: 0.5 g
VM3003:103 calculatedProtein is: 1.5 g
VM3003:105 amount of carbs in 100g is: 13 g
VM3003:106 amount carbs in fried rice is: 39 g
VM3003:108 amount of fiber in 100g is: 0.1 g
VM3003:109 amount fiber in fried rice is: 0.3 g
VM3003:111 amount of fat in 100g is: 1.4 g
VM3003:112 amount fat in fried rice is: 4.199999999999999 g
VM3003:114 amount of iron in 100mg is: 0.3 mg
VM3003:115 amount iron in fried rice is: 0.9 mg
VM3003:117 amount of sodium in 100mg is: 1 mg
VM3003:118 amount sodium in fried rice is: 3 mg
VM3003:120 amount of calcium in 100mg is: 13 mg
VM3003:121 amount calcium in fried rice is 39 mg
VM3003:123 amount of vitamin D in 100µg is 0 µg
VM3003:124 amount vitamin D in fried rice is 0 µg
VM3003:126 amount of calories in 100g is: 50 kcal
VM3003:127 amount of calories in fried rice is: 150 kcal
VM3003:129 these are my totals for smoothie:
VM3003:130 total weight for fried rice ingredients 290 g
VM3003:131 total amount of fried rice protein: 6.336 g
VM3003:132 total amount of fried rice carbs: 48.76199999999999 g
VM3003:133 total amount of fried rice fiber: 1.5939999999999999 g
VM3003:134 total amount of fried rice fat: 9.940000000000001 g
VM3003:135 total amount of fried rice Iron: 66.78399999999999 mg
VM3003:136 total amount of fried rice sodium: 147.2 mg
VM3003:137 total amount of fried rice calcium: 18.68 mg
VM3003:138 total amount of fried rice vitamin D: 0.78 µg
VM3003:139 total amount of calories in fried rice is: 187.7 kcal
  • lucas-tam-nutrition-calculator.txt
  • Last modified: 2022/06/21 23:59
  • by lucas.tam