-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
102 lines (90 loc) · 2.47 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var app = new Vue({
el:"#app",
data(){
return{
nutrition:{
Energy:0,
Cholesterol:0,
CalciumCa:0,
TotalFat:0,
Sodium:0,
Carbohydrate:0,
Protein:0,
Iron:0,
Pottassium:0,
VitaminC:0,
},
noServe:1,
recipe:"",
show:true
}
},
methods:{
clearAll(){
this.nutrition.Energy=0
this.nutrition.Cholesterol=0
this.nutrition.CalciumCa=0
this.nutrition.TotalFat=0
this.nutrition.Sodium=0
this.nutrition.Carbohydrate=0
this.nutrition.Protein=0
this.nutrition.Iron=0
},
//function for separating the input Recipe into list of words separated by \n escape sequence and store it in array named arrayOfItems.
//Itrate through each element of array. Search for that item in the FoodData Central database using api and return the nutrition details of first search result. Store it.
//Itrate through properties of each Nutrient and find needed field and store needed data.
async getNutri(){
var noServe=parseInt(this.noServe)//Change the type of selected number of serving to integer and store in local variable noServe
var arrayOfItems =""
// differentiate and store the recipe items from value of textarea by \n as separator
arrayOfItems =this.recipe.split("\n")
// loop through list of recipe items
for(var i in arrayOfItems){
var a= arrayOfItems[i]
await axios.get(`https://api.nal.usda.gov/fdc/v1/foods/search?dataType=Branded&api_key=dJ8aIYIXXO2r3hykzELHmhx3maH4hAuD0tvfSg04&query=${a}`)
.then((response)=>{
var totalNutri=response.data.foods[0].foodNutrients
// Loop through the list of nutrients
for(var i in totalNutri){
switch(totalNutri[i].nutrientName){
case "Total lipid (fat)":{
this.nutrition.TotalFat+=(totalNutri[i].value*noServe)
}
break;
case "Protein":{
this.nutrition.Protein+=(totalNutri[i].value*noServe)
}
break;
case "Carbohydrate, by difference":{
this.nutrition.Carbohydrate+=(totalNutri[i].value*noServe)
}
break;
case "Energy":{
this.nutrition.Energy+=(totalNutri[i].value*noServe)
}
break;
case "Sodium, Na":{
this.nutrition.Sodium+=(totalNutri[i].value*noServe)
}
break;
case "Calcium, Ca":{
this.nutrition.CalciumCa+=(totalNutri[i].value*noServe)
}
break;
case "Vitamin C, total ascorbic acid":{
this.nutrition.VitaminC+=(totalNutri[i].value*noServe)
}
break;
case "Iron, Fe":{
this.nutrition.Iron+=(totalNutri[i].value*noServe)
}
break;
}
}
},(error)=>{
console.log(error);
});
}
}
}
})