Table of Contents

fried rice nutrition calculator

By: Yuna Wu

Writing 文章

English Version 英文版

Have you tried to use program to make a calculator? This semester, we use programming to make a nutrition calculator. I will talk about what I did and what I learned during the time that I was doing the nutrition calculator.

Let me tell you how did I make this nutrition calculator. First, I use a class called “Ingredient” to write down the ingredient. Then I make a function called “calculateProteinCarb” to calculate the nutrition. This part is the part that included the most mathematical and calculation parts. After that, I use “let” to call the function, and I use “sum” function to add all ingredients. And then I make a printer function to print out the result.

I learn how to use “sum” function to add all ingredients, and I also learn how to call functions at one time through doing this work. I also learn about science through doing this work. I learn how many ingredients the food has, and I learn there has a special way to calculate ingredients. I also learn about math through doing this work. Do this work need to understand how to calculate the ingredients. I learn how to calculate the ingredients. I also learn how to turn math into programming. Actually, it took me a long time to understand the math and make it into a program. And it also took me a long time to understand the things that Dr.Bell told me. In math, my comprehension has always been poor and I often don't understand it, so it really takes me a lot of time. So every time I understand or make it, I will feel a sense of accomplishment and feel very happy.

Through doing this work, I know that I can do some programming by myself. I also know that sometime I should try to figure it out by myself more. But sometimes I really need people to help me because I am really easy to mess up by myself. When I have trouble with my program, I always get help from Dr.Bell. Every time I get help from Dr.Bell, he will ask me where I have trouble. I will tell him the place where I have trouble, and Dr.Bell always will help me to figure it out, and try to let me understand it.

I think in the future, I can use this skill to do maths easily, and quickly. I think in the future I can try to do this more, and be more careful with the spelling part. In the future programming is a really useful thing, I think everybody should learn some of it, so learn some programming together with me!

Chinese Version 中文版

你有沒有試過用程式的方式來做一個計算機呢?在這個學期,我們用程式來做了一個計算營養成分的計算機。在這篇文章中,我會講到我在做這個計算機的期間所做的事情還有我所學到了東西。

請先讓我來告訴你我是怎麼製作這個計算機的。首先,我做了一個class叫做“Ingredient”來寫下成分。接下來,我做了一個叫做“calculateProteinCarb”的函數來計算營養成分。這個部分是有最多的數學和計算的部分。之後,我用“let”來呼叫函數,並且使用“sum”函數把所有的營養成分加在一起。然後,我做了一個“printer”函數來把結果印出來。

我學會了怎麼用“sum”函數來把所有的營養成分加在一起,我還在這次的功課中學會了怎麼一次性呼叫函數。通過這次的功課我還有學到有關於科學的知識。我了解到了食物的不同成分,還知道了要計算成分是有一種特別的計算公式的。通過這次的功課我還有學到關於數學的知識。做這項功課需要了解怎麼計算營養成分,所以我學會了怎麼樣去計算營養成分。我還學會了怎麼把數學變成程式。實際上,我花了蠻久的時間去理解數學然後把數學轉換成程式。而且我也花了蠻長的時間去理解Bell老師告訴我的事情。在數學這方面我的理解能力一直很差,經常不懂老師講的數學,所以真的會花我蠻多時間的。所以,每次我理解或者是我做出來的時候,我都會有一種成就感,會覺得很開心。

通過這項功課,我知道我可以靠著自己做一些程式。我也知道有的時候我應該自己嘗試去弄明白。但是我有的時候是真的需要別人的幫忙,因為我自己很容易搞砸。每次我的程序出問題的時候,我總是去找Bell老師幫忙。每次我從Bell老師那裡得到幫助的時候,他都會問我我的程序哪裡出錯了。我告訴他我遇到了困難的地方,Bell老師總是幫忙我解決我的問題,然後試圖去讓我理解它。

我覺得在未來,我可以用這個技能來讓我的數學做得更容易,更快。我覺得在未來我可以嘗試做更多這種東西,然後在拼寫方面可以加強一下,不要老是打錯字。在未來,程序是一個非常有用的技能,我覺得每個人都應該多多少少要學一點,所以一起來跟我學習程序吧!

Code

class Ingredient {
    constructor (name, proteinAmount100g, amountInDish, carbAmount100g, fatAmount100g, fiberAmount100g, ironAmount100g, sodiumAmount100g, calciumAmount100g, vitaminDAmount100g, caloriesAmount100g)
    { this.name = name;
      this.proteinAmount100g = proteinAmount100g;
      this.amountInDish = amountInDish;
      this.calculatedProtein = undefined;
      //carb
      this.carbAmount100g = carbAmount100g; //carb = 碳水化合物
      this.calculatedCarb = undefined;
      //fat
      this.fatAmount100g = fatAmount100g;
      this.calculatedFat = undefined;
      //fiber
      this.fiberAmount100g = fiberAmount100g; //fiber = 纖維
      this.calculatedFiber = undefined;
      //iron
      this.ironAmount100g = ironAmount100g;//mg
      this.calculatedIron = undefined;
      //sodium
      this.sodiumAmount100g = sodiumAmount100g;//mg
      this.calculatedSodium = undefined;
      //calcuim
      this.calciumAmount100g = calciumAmount100g;//mg
      this.calculatedCalcium = undefined;
      //vitamin D
      this.vitaminDAmount100g = vitaminDAmount100g;
      this.calculatedVitaminD = undefined;
      //calories
      this.caloriesAmount100g = caloriesAmount100g;
      this.calculatedCalories = undefined;
    }
      }


function calculateProteinCarb (ingredientObject) {
  ingredientObject.calculatedProtein = ingredientObject.proteinAmount100g / 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedCarb = ingredientObject.carbAmount100g / 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedFat = ingredientObject.fatAmount100g / 100 * 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedFiber = ingredientObject.fiberAmount100g / 100 * 100 *ingredientObject.amountInDish;
  ingredientObject.calculatedIron = ingredientObject.ironAmount100g / 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedSodium = ingredientObject.sodiumAmount100g / 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedCalcium = ingredientObject.calciumAmount100g / 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedVitaminD = ingredientObject.vitaminDAmount100g / 100 * ingredientObject.amountInDish;
  ingredientObject.calculatedCalories = ingredientObject.caloriesAmount100g / 100 * ingredientObject.amountInDish;
  return ingredientObject
}


let allIngredient = [
  new Ingredient('carrot', 0.9, 25, 9.6, 0.2, 2.8, 0.3, 69, 33, 0, 41),
  new Ingredient('rice', 2.7, 450, 28, 0.3, 0.4, 1.2, 1, 10, 0, 130),
  new Ingredient('peas', 5.4, 10, 15, 0.4, 5.7, 1.5, 5, 25, 0, 81),
  new Ingredient('Roasted Ham', 27, 25, 0, 18, 0, 1, 60, 14, 0.8, 273),
]

allIngredient = allIngredient.map(calculateProteinCarb)
console.log(allIngredient)

//calculateProtein(allIngredient[0])
//calculateProtein(allIngredient[1])
//calculateProtein(allIngredient[2])
//calculateProtein(allIngredient[3])

function sumArray (inputArray) {
  let sum = 0;
  for (let i = 0; i < inputArray.length; i++) {
    sum = sum + inputArray[i]
  }
  return sum}

let allProtein = allIngredient.map(ingredientObject => ingredientObject.calculatedProtein)
let totalProteins  = sumArray(allProtein)
//not finish
let allCarb = allIngredient.map(ingredientObject => ingredientObject.calculatedCarb)
let totalCarbs  = sumArray(allCarb)
//not finish
let allFat = allIngredient.map(ingredientObject => ingredientObject.calculatedFat)
let totalFats  = sumArray(allFat)
//not finish
let allFiber = allIngredient.map(ingredientObject => ingredientObject.calculatedFiber)
let totalFibers  = sumArray(allFiber)
//not finish
let allIron = allIngredient.map(ingredientObject => ingredientObject.calculatedIron)
let totalIrons  = sumArray(allIron)
//not finish
let allSodium = allIngredient.map(ingredientObject => ingredientObject.calculatedSodium)
let totalSodiums  = sumArray(allSodium)
//not finish
let allCalcium = allIngredient.map(ingredientObject => ingredientObject.calculatedCalcium)
let totalCalciums  = sumArray(allCalcium)
//not finish
let allVitaminD = allIngredient.map(ingredientObject => ingredientObject.calculatedVitaminD)
let totalVitaminDs  = sumArray(allVitaminD)
//not finish
let allCalories = allIngredient.map(ingredientObject => ingredientObject.calculatedCalories)
let totalCalories  = sumArray(allCalories)
//weight
let allWeight = allIngredient.map(ingredientObject => ingredientObject.amountInDish)
let totalWeight  = sumArray(allWeight)



//this one is not finish.
function ingredientPrinter (inputArray, totalProteins, totalCarbs, totalFats, totalFibers, totalIrons, totalSodiums, totalCalciums, totalVitaminDs, totalCalories) {
  inputArray.forEach(i =>{console.log("The name of the food is",i.name);
  console.log("the amount of protein is", i.proteinAmount100g, "g");
  console.log("the amount of dish is ", i.amountInDish, "g");
  console.log("the amount of carb is", i.carbAmount100g, "g");
  console.log("the protein is ", i.calculatedProtein, "g");
  console.log("the total carb is ", i.calculatedCarbs, "g");
  console.log("the total fat is ", i.calculatedFat, "g");
  console.log("the total fiber is ", i.calculatedFiber, "g");
  console.log("the total iron is ", i.calculatedIron, "mg");
  console.log("the total sodium is ", i.calculatedSodium, "mg");
  console.log("the total calcium is ", i.calculatedCalcium, "mg");
  console.log("the total vitamin D is ", i.calculatedVitaminD, "μg");
  console.log("the total calories is ", i.calculatedCalories, "g");
}
)
}


function totalPrinter (name, inputArray, totalWeight, totalProteins, totalCarbs, totalFats, totalFibers, totalIrons, totalSodiums, totalCalciums, totalVitaminDs, totalCalories) {
  console.log("The name of the food is", name);
  console.log("the total weight of the fried rice is", totalWeight, "g");
  console.log("the total protein of the fried rice is", totalProteins, "g");
  console.log("the total carb of the fried rice is", totalCarbs, "g");
  console.log("the total fat of the fried rice is", totalFats, "g");
  console.log("the total fiber of the fried rice is", totalFibers, "g");
  console.log("the total iron of the fried rice is", totalIrons, "mg");
  console.log("the total sodium of the fried rice is", totalSodiums, "mg");
  console.log("the total calcium of the fried rice is", totalCalciums, "mg");
  console.log("the total Vitamin D of the fried rice is", totalVitaminDs, "μg");
  console.log("the total calories of the fried rice is", totalCalories, "g");
}

ingredientPrinter(allIngredient, totalProteins, totalCarbs, totalFats, totalFibers, totalIrons, totalSodiums, totalCalciums, totalVitaminDs, totalCalories)

totalPrinter("fried Rice", allIngredient, totalWeight, totalProteins, totalCarbs, totalFats, totalFibers, totalIrons, totalSodiums, totalCalciums, totalVitaminDs, totalCalories)

Back to Yuna Wu's Page