=====nutrition calculator=====
by [[oliver-wang|oliver wang]]
=====We made a nutrition calculator by ourselves!=====
In this semester we've made a lot of thing with coding. Like we make math and programming mix together and drawing with p5js or SVG.JS. We also have a main goal for learning the programming in this semester, and the goal is to make a nutrition calculator.
The purpose of this nutriotion calculator is to make people to calculate any grams they want, and choose the ingredient that they want to use. Also the way that this “nutrition calculator” work is I use a lot of tools called “class” to make it, and what class do is helping us to collect every data we want so when we need the data of something it will be easy to find. For the calulation part we will need to study something about nutrition, like how to calculate nutrition need to divide by what number of the nutrition. also if there have a lot of calculation, then we will use a tools called “for each”, and what for each does is it help us to calculate a math over and over.
After I finish this “nutrition calculator” I feel very happy for myself, because I feel like I've learn a lot of thing, no matter its about science, math and programming. What programming skills I've understand more tools about javascript, the tools I've learned is for each, map, for loop and some part of math how to do inside coding. Also I've learn something about nutrition calculation a lot, because before I put all of the calculation in this “nutrition calculator” I must understand the math and science inside it first so I feel like I learned a lot about math and science.
In this project I don't feel like I only just leaned about math or programming I also learned some kind of skill and myself. What I learned about in this project is when I look at our own code I must check it properly and also be patient, because if I got angry or sad or some other emotional feeling I will be more harder to find those issue. Also what I've learned about I'm very easy to get distracted and I'm really hard to just sit there and to find the issue whole class, so if is that case I'll just stand up and stretch.
In this project I feel like I really learned a lot of thing, no matter what is it. The example is how to be patiant, how to not get distracted and something else. I think when we have a goal that we need to reach, then I feel most peolple will have higer efficiency to reach that goal and learn better.
===code編碼===
class NutritionData {
constructor(name, proteinInHundredGrams,fatsInHundredGrams, carbsInHundredGrams, caloriesInHundredGrams, fiberInHundredGrams, ironInHundredGrams, sodiumInHundredGrams, calciumInHundredGrams, vitaminCInHundredGrams, vitaminDInHundredGrams){
this.name = name;
this.protein = proteinInHundredGrams;
this.fats = fatsInHundredGrams;
this.carbs = carbsInHundredGrams;
this.calories = caloriesInHundredGrams;
this.fiber = fiberInHundredGrams;
this.iron = ironInHundredGrams/1000;
this.sodium = sodiumInHundredGrams/1000;
this.calcium = calciumInHundredGrams/1000;
this.vitaminC = vitaminCInHundredGrams/1000;
this.vitaminD = vitaminDInHundredGrams*(10**-6);
}
}
class Ingredient{
constructor(weight, nutritionDataObject){
this.name = nutritionDataObject.name;
this.protein = nutritionDataObject.protein * (weight/100);
this.fats = nutritionDataObject.fats * (weight/100);
this.carbs = nutritionDataObject.carbs * (weight/100);
this.calories = nutritionDataObject.calories * (weight/100)
this.fibers = nutritionDataObject.fiber * (weight/100)
this.iron = nutritionDataObject.iron * (weight/100)
this.sodiums = nutritionDataObject.sodium * (weight/100)
this.calciums = nutritionDataObject.calcium * (weight/100)
this.vitaminC = nutritionDataObject.vitaminC * (weight/100)
this.vitaminD = nutritionDataObject.vitaminD * (weight/100)
this.weight = weight
}
}
function sumArray (inputArray) {
let sum = 0;
for(let i = 0 ; i < inputArray.length; i++){
sum = sum + inputArray[i]
}
return sum
}
class Dish {
constructor(dishName,servings, ingredientObject){
//put array of ingredient inside a property
this.dishName = dishName;
this.totalWei = sumArray(ingredientObject.map(x => x.weight/servings))
this.totalPro = sumArray(ingredientObject.map(x => x.protein/servings));
this.totalFat = sumArray(ingredientObject.map(x => x.fats/servings));
this.totalCar = sumArray(ingredientObject.map(x => x.carbs/servings));
this.totalCal = sumArray(ingredientObject.map(x => x.calories/servings));
this.totalFib = sumArray(ingredientObject.map(x => x.fibers/servings));
this.totalIro = sumArray(ingredientObject.map(x => x.iron/servings));
this.totalSod = sumArray(ingredientObject.map(x => x.sodiums/servings));
this.totalCalc = sumArray(ingredientObject.map(x => x.calciums/servings));
this.totalVitC = sumArray(ingredientObject.map(x => x.vitaminC/servings));
this.totalVitD = sumArray(ingredientObject.map(x => x.vitaminD/servings));
}
}
function showing (word){
return " " + word.name + " (" + word.weight + "g)"
}
class PrettyPrintedNutrition {
//put strings in properties
constructor(dishObject, ingredients){
//put arrays for "ingredients"
this.name = `The dish that we're calculating is ${dishObject.dishName}.`
this.thoseIngredient = `The ingredients inside this ${dishObject.dishName} are ${ingredients.map(showing)}.`
this.totalWei = `The total weight of the ${dishObject.dishName} is ${dishObject.totalWei}g.`;
this.totalPro = `The total protein inside ${dishObject.dishName} is ${dishObject.totalPro}g.`;
this.totalFat = `The total fats inside ${dishObject.dishName} is ${dishObject.totalFat}g.`;
this.totalCar = `The total carbs inside ${dishObject.dishName} is ${dishObject.totalCar}g.`;
this.totalCal = `The total calories inside ${dishObject.dishName} is ${dishObject.totalCal} cal.`;
this.totalFib = `The total fiber inside ${dishObject.dishName} is ${dishObject.totalFib}g.`;
this.totalIro = `The total iron inside ${dishObject.dishName} is ${dishObject.totalFib}g.`;
this.totalSod = `The total sodium inside ${dishObject.dishName} is ${dishObject.totalSod}g.`;
this.totalCalc = `The total calcium inside ${dishObject.dishName} is ${dishObject.totalCalc}g.`;
this.totalVitC = `The total vitaminC inside ${dishObject.dishName} is ${dishObject.totalVitC}g.`;
this.totalVitD = `The total vitaminD inside ${dishObject.dishName} is ${dishObject.totalVitD}g.`;
this.ingredients = ingredients
}printing(){
console.log(this.name)
console.log(`The nutrition inside:`);
for(let i = 0 ; i < this.ingredients.length; i++){
console.log(`${this.ingredients[i].name}`)
console.log(`weight:${this.ingredients[i].weight}g
protein:${this.ingredients[i].protein}g
fats:${this.ingredients[i].fats}g
carbs:${this.ingredients[i].carbs}g
calories:${this.ingredients[i].calories}g
fibers:${this.ingredients[i].fibers}g
iron:${this.ingredients[i].iron}g
sodium:${this.ingredients[i].sodiums}g
calcium:${this.ingredients[i].calciums}g
vitaminC:${this.ingredients[i].vitaminC}g
vitaminD:${this.ingredients[i].vitaminD}g
`)
}
console.log(this.totalWei);
console.log(this.totalCal);
console.log(this.totalPro);
console.log(this.totalFat);
console.log(this.totalCar);
console.log(this.totalFib);
console.log(this.totalIro);
console.log(this.totalSod);
console.log(this.totalCalc);
console.log(this.totalVitC);
console.log(this.totalVitD);
console.log(this.thoseIngredient);
}
}
let vegetarianNutritionList = [
new NutritionData ("butter", 0.9, 81, 0.1, 717, 0, 0, 11, 24, 0, 0),
new NutritionData ("fried eggs", 14, 15, 0.8, 196, 0, 1.9, 207, 62, 0, 2.2),
new NutritionData ("carrots", 0.9, 0.2, 9.6, 41, 2.8, 0.3, 69, 33, 5.9, 0),
new NutritionData ("Caldwell & Son Inc. - Diced White Onion",1.2, 0, 9.4, 41, 1.2, 0, 0, 24, 7.1, 0),
new NutritionData ("Grenway - Green Peas", 5.6, 0, 12, 79, 4.5, 1.2, 11, 0, 6.7, 0),
new NutritionData ("garlic", 6.4, 0.5, 33, 149, 2.1, 1.7, 17, 181, 31.2, 0),
new NutritionData ("cooked white rice", 2.7, 0.3, 28, 130, 0.4, 1.2, 1, 10, 0, 0),
new NutritionData ("Growers Marketing LLC - Fresh Green Onions", 0, 0, 8, 40, 4, 1.4, 40, 80, 19.2, 0),
new NutritionData ("soy sauce", 8.1, 0.6, 4.9, 53, 0.8, 1.5, 5493, 33, 0, 0),
new NutritionData ("oyster sauce", 5.5, 8.1, 7.8, 127, 0.5, 3.6, 242, 79, 3.3, 0.6),
new NutritionData ("sesame Oil", 0, 100, 0, 884, 0, 0, 0, 0, 0, 0)
]
let ingredientArray = [
new Ingredient (42, vegetarianNutritionList[0]),
new Ingredient (88, vegetarianNutritionList[1]),
new Ingredient (120, vegetarianNutritionList[2]),
new Ingredient (125, vegetarianNutritionList[3]),
new Ingredient (70, vegetarianNutritionList[4]),
new Ingredient (18, vegetarianNutritionList[5]),
new Ingredient (744, vegetarianNutritionList[6]),
new Ingredient (36, vegetarianNutritionList[7]),
new Ingredient (56, vegetarianNutritionList[8]),
new Ingredient (12, vegetarianNutritionList[9]),
new Ingredient (2.25, vegetarianNutritionList[10])
]
let vegetarianFood = new Dish("vegetarian fried rice", 5, ingredientArray)
let printNutrition = new PrettyPrintedNutrition(vegetarianFood, ingredientArray)
printNutrition.printing()
====Explanation解釋編碼====
===Nutrition data===
What this code does is to help us collect every data we want inside [[https://tools.myfooddata.com/nutrition-facts|My food data]]
這個編碼的作用是幫我們整理所有我們從[[https://tools.myfooddata.com/nutrition-facts|My food data]]取出來的資料。
class NutritionData {
constructor(name, proteinInHundredGrams,fatsInHundredGrams, carbsInHundredGrams, caloriesInHundredGrams, fiberInHundredGrams, ironInHundredGrams, sodiumInHundredGrams, calciumInHundredGrams, vitaminCInHundredGrams, vitaminDInHundredGrams){
this.name = name;
this.protein = proteinInHundredGrams;
this.fats = fatsInHundredGrams;
this.carbs = carbsInHundredGrams;
this.calories = caloriesInHundredGrams;
this.fiber = fiberInHundredGrams;
this.iron = ironInHundredGrams/1000;
this.sodium = sodiumInHundredGrams/1000;
this.calcium = calciumInHundredGrams/1000;
this.vitaminC = vitaminCInHundredGrams/1000;
this.vitaminD = vitaminDInHundredGrams*(10**-6);
}
}
===Igredient===
This code is helping us to calculate and collect how much nutrition after we divide by the weight we want.
這個編碼是在幫助我們計算然後收集我們除以完我們希望的重量以後的營養的數值。
class Ingredient{
constructor(weight, nutritionDataObject){
this.name = nutritionDataObject.name;
this.protein = nutritionDataObject.protein * (weight/100);
this.fats = nutritionDataObject.fats * (weight/100);
this.carbs = nutritionDataObject.carbs * (weight/100);
this.calories = nutritionDataObject.calories * (weight/100)
this.fibers = nutritionDataObject.fiber * (weight/100)
this.iron = nutritionDataObject.iron * (weight/100)
this.sodiums = nutritionDataObject.sodium * (weight/100)
this.calciums = nutritionDataObject.calcium * (weight/100)
this.vitaminC = nutritionDataObject.vitaminC * (weight/100)
this.vitaminD = nutritionDataObject.vitaminD * (weight/100)
this.weight = weight
}
}
===sumArray and dish===
This two code just help us to calculate and collect the total.
這個編碼只是幫助我們計算以及統整所有營養數值的總計。
function sumArray (inputArray) {
let sum = 0;
for(let i = 0 ; i < inputArray.length; i++){
sum = sum + inputArray[i]
}
return sum
}
class Dish {
constructor(dishName,servings, ingredientObject){
//put array of ingredient inside a property
this.dishName = dishName;
this.totalWei = sumArray(ingredientObject.map(x => x.weight/servings))
this.totalPro = sumArray(ingredientObject.map(x => x.protein/servings));
this.totalFat = sumArray(ingredientObject.map(x => x.fats/servings));
this.totalCar = sumArray(ingredientObject.map(x => x.carbs/servings));
this.totalCal = sumArray(ingredientObject.map(x => x.calories/servings));
this.totalFib = sumArray(ingredientObject.map(x => x.fibers/servings));
this.totalIro = sumArray(ingredientObject.map(x => x.iron/servings));
this.totalSod = sumArray(ingredientObject.map(x => x.sodiums/servings));
this.totalCalc = sumArray(ingredientObject.map(x => x.calciums/servings));
this.totalVitC = sumArray(ingredientObject.map(x => x.vitaminC/servings));
this.totalVitD = sumArray(ingredientObject.map(x => x.vitaminD/servings));
}
}
===showing and pretty printed nurition===
showing function is helping us to show how much weight in each ingredient, and the pretty printed nutrition is helping us to print everything out.
showing 函數的作用是幫我們去打印出在每個食材中有多少重量,然後 pretty printed nutrition 的作用就是把所有的營養數值都打印出來。
function showing (word){
return " " + word.name + " (" + word.weight + "g)"
}
class PrettyPrintedNutrition {
//put strings in properties
constructor(dishObject, ingredients){
//put arrays for "ingredients"
this.name = `The dish that we're calculating is ${dishObject.dishName}.`
this.thoseIngredient = `The ingredients inside this ${dishObject.dishName} are ${ingredients.map(showing)}.`
this.totalWei = `The total weight of the ${dishObject.dishName} is ${dishObject.totalWei}g.`;
this.totalPro = `The total protein inside ${dishObject.dishName} is ${dishObject.totalPro}g.`;
this.totalFat = `The total fats inside ${dishObject.dishName} is ${dishObject.totalFat}g.`;
this.totalCar = `The total carbs inside ${dishObject.dishName} is ${dishObject.totalCar}g.`;
this.totalCal = `The total calories inside ${dishObject.dishName} is ${dishObject.totalCal} cal.`;
this.totalFib = `The total fiber inside ${dishObject.dishName} is ${dishObject.totalFib}g.`;
this.totalIro = `The total iron inside ${dishObject.dishName} is ${dishObject.totalFib}g.`;
this.totalSod = `The total sodium inside ${dishObject.dishName} is ${dishObject.totalSod}g.`;
this.totalCalc = `The total calcium inside ${dishObject.dishName} is ${dishObject.totalCalc}g.`;
this.totalVitC = `The total vitaminC inside ${dishObject.dishName} is ${dishObject.totalVitC}g.`;
this.totalVitD = `The total vitaminD inside ${dishObject.dishName} is ${dishObject.totalVitD}g.`;
this.ingredients = ingredients
}printing(){
console.log(this.name)
console.log(`The nutrition inside:`);
for(let i = 0 ; i < this.ingredients.length; i++){
console.log(`${this.ingredients[i].name}`)
console.log(`weight:${this.ingredients[i].weight}g
protein:${this.ingredients[i].protein}g
fats:${this.ingredients[i].fats}g
carbs:${this.ingredients[i].carbs}g
calories:${this.ingredients[i].calories}g
fibers:${this.ingredients[i].fibers}g
iron:${this.ingredients[i].iron}g
sodium:${this.ingredients[i].sodiums}g
calcium:${this.ingredients[i].calciums}g
vitaminC:${this.ingredients[i].vitaminC}g
vitaminD:${this.ingredients[i].vitaminD}g
`)
}
console.log(this.totalWei);
console.log(this.totalCal);
console.log(this.totalPro);
console.log(this.totalFat);
console.log(this.totalCar);
console.log(this.totalFib);
console.log(this.totalIro);
console.log(this.totalSod);
console.log(this.totalCalc);
console.log(this.totalVitC);
console.log(this.totalVitD);
console.log(this.thoseIngredient);
}
}
===object===
This part is the way that we can call this calculator.
這個部分是一種我們用這個計算機的方式。
let vegetarianNutritionList = [
new NutritionData ("butter", 0.9, 81, 0.1, 717, 0, 0, 11, 24, 0, 0),
new NutritionData ("fried eggs", 14, 15, 0.8, 196, 0, 1.9, 207, 62, 0, 2.2),
new NutritionData ("carrots", 0.9, 0.2, 9.6, 41, 2.8, 0.3, 69, 33, 5.9, 0),
new NutritionData ("Caldwell & Son Inc. - Diced White Onion",1.2, 0, 9.4, 41, 1.2, 0, 0, 24, 7.1, 0),
new NutritionData ("Grenway - Green Peas", 5.6, 0, 12, 79, 4.5, 1.2, 11, 0, 6.7, 0),
new NutritionData ("garlic", 6.4, 0.5, 33, 149, 2.1, 1.7, 17, 181, 31.2, 0),
new NutritionData ("cooked white rice", 2.7, 0.3, 28, 130, 0.4, 1.2, 1, 10, 0, 0),
new NutritionData ("Growers Marketing LLC - Fresh Green Onions", 0, 0, 8, 40, 4, 1.4, 40, 80, 19.2, 0),
new NutritionData ("soy sauce", 8.1, 0.6, 4.9, 53, 0.8, 1.5, 5493, 33, 0, 0),
new NutritionData ("oyster sauce", 5.5, 8.1, 7.8, 127, 0.5, 3.6, 242, 79, 3.3, 0.6),
new NutritionData ("sesame Oil", 0, 100, 0, 884, 0, 0, 0, 0, 0, 0)
]
let ingredientArray = [
new Ingredient (42, vegetarianNutritionList[0]),
new Ingredient (88, vegetarianNutritionList[1]),
new Ingredient (120, vegetarianNutritionList[2]),
new Ingredient (125, vegetarianNutritionList[3]),
new Ingredient (70, vegetarianNutritionList[4]),
new Ingredient (18, vegetarianNutritionList[5]),
new Ingredient (744, vegetarianNutritionList[6]),
new Ingredient (36, vegetarianNutritionList[7]),
new Ingredient (56, vegetarianNutritionList[8]),
new Ingredient (12, vegetarianNutritionList[9]),
new Ingredient (2.25, vegetarianNutritionList[10])
]
let vegetarianFood = new Dish("vegetarian fried rice", 5, ingredientArray)
let printNutrition = new PrettyPrintedNutrition(vegetarianFood, ingredientArray)
printNutrition.printing()