-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseRecipes.js
63 lines (50 loc) · 1.63 KB
/
parseRecipes.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
var allIngredients = ["Apples", "Cherries", "Honey", "Peaches", "Blueberries", "Lemons", "Broccoli",
"Melons", "Carrots", "Strawberries", "Cucumbers", "Pumpkin", "Chili",
"Grapes", "Onions", "Tomatoes", "Pepper", "Cabbage", "Oranges", "Coffee"];
function mapByIngredients(ingredientList, cookbook) {
var ingredientDict = {};
ingredientList.forEach ( function (ingredient) {
ingredientDict[ingredient] = cookbook.filter( (recipe) => {
if (typeof recipe["ingredients"] != "undefined") {
var found = recipe["title"].indexOf(ingredient.slice(0, ingredient.length - 1));
return (found != -1);
}
return false;
})
});
console.log(ingredientDict);
return ingredientDict;
}
function printRecipe(div, crop) {
var recipe = randomRecipe(crop, recipeByIngredient);
div.selectAll("p").remove();
div.selectAll("h3").remove();
div.selectAll("img").remove();
div.selectAll("g").remove();
div.append("p")
.attr("class", "title")
.attr("font-size", "3em")
.text(recipe["title"].toUpperCase());
div.append("p")
.attr("class", "title")
.text(("Ingredients"));
recipe["ingredients"].forEach( (d) => {
div.append("p")
.style("margin-bottom", 0)
.text(d);
});
div.append("p")
.attr("class", "title")
.style("margin-top", 10)
.text(("Directions"));
recipe["directions"].forEach( (d) => {
div.append("p")
.style("margin-bottom", 0)
.text(d);
});
div.selectAll("p").style("margin-left", 15);
}
function randomRecipe(ingredient, cookbook) {
var chapter = cookbook[ingredient.charAt(0).toUpperCase() + ingredient.slice(1, ingredient.length)];
return chapter[Math.floor(Math.random() * chapter.length)];
}