-
Notifications
You must be signed in to change notification settings - Fork 2
/
grep.js
154 lines (127 loc) · 3.51 KB
/
grep.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* The first argument is the list of ingredients in the recipe in our database.
* The second argument is the list of ingredients that the user has.
*/
function compareIngredient(recipeIngre, userIngre) {
var i;
for (i = 0; i < recipeIngre.length; i++) {
if (userIngre.includes(recipeIngre[i]) == false) return 0;
}
return 1;
}
/*
* The first argument is the list of tags in the recipe in our database.
* The second argument is the list of tags that the user has.
*/
function compareTag(recipeTag, userTag) {
var i;
for (i = 0; i < userTag.length; i++) {
if (recipeTag.includes(userTag[i]) == false) return 0;
}
return 1;
}
/*
* jsonObj: the json object storing the recipe
* requirements: user input. Format:
* {
* "ingredients": [],
* "calories": "",
* "tags": []
* }
*/
function selectRecipe(jsonObj, requirements) {
// All recipes.
let allRecipes = jsonObj['recipes'];
// All ingredients.
let ingredients = requirements['ingredients'];
// All tags.
let tags = requirements['tags'];
// Selection of calory.
let calories = requirements['calories'];
// A list to store all available recipes after the first round of selection:
// looking for ingredients.
var firstRoundList = [];
let i = 0;
// Traverse the list of recipes and filter ingredients.
for (i = 0; i < recipes.length; i++) {
var recipe = recipes[i];
if (compareIngredient(recipe['ingredients'], ingredients) == 1) {
firstRoundList = firstRoundList.concat(recipe);
}
}
if (firstRoundList.length == 0) return [];
// Start the second round, matching tags.
var secondRoundList = [];
// Traverse the list of tags and filter ingredients.
for (i = 0; i < firstRoundList.length; i++) {
var recipe = firstRoundList[i];
if (compareTag(recipe['tags'], tags) == 1) {
secondRoundList = secondRoundList.concat(recipe);
}
}
if (secondRoundList.length == 0) return [];
// Start the third round.
var low = [];
var medium = [];
var high = [];
// Firstly, sort the recipe based on calory from the resulted list.
for (i = 0; i < secondRoundList.length; i++) {
var recipe = secondRoundList[i];
if (recipe['calories'].toUpperCase() == "LOW") {
if (low.length != 0) low += ", ";
low += JSON.stringify(recipe);
}
else if (recipe['calories'].toUpperCase() == "MEDIUM") {
if (medium.length != 0) medium += ", ";
medium += JSON.stringify(recipe);
}
else {
if (high.length != 0) high += ", ";
high += JSON.stringify(recipe);
}
}
// Then, get the list based on user's preference.
var retList = [];
if (calories.toUpperCase() == "LOW") {
retList += low;
if (medium.length != 0) {
if (retList.length != 0) retList += ", ";
retList += medium;
}
}
else if (calories.toUpperCase() == "MEDIUM") {
retList += medium;
if (low.length != 0) {
if (retList.length != 0) retList += ", ";
retList += low;
}
if (high.length != 0) {
if (retList.length != 0) retList += ", ";
retList += high;
}
}
else {
retList += high;
if (medium.length != 0) {
if (retList.length != 0) retList += ", ";
retList += medium;
}
}
return retList;
}
/*
* The first argument is the name of the recipe.
* The second argument is the json obejct.
*/
function parseInstruction(recipe, jsonObj) {
// All instructions.
let allRecipes = jsonObj['theInstructions'];
var ret = [];
// Travser the instructionFile and get the one in the recipe.
for (let i = 0; i < theInstructions.length; i++) {
if (recipe == theInstructions[i]['name']) {
ret += JSON.stringify(theInstructions[i]);
}
}
return ret;
}