-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
71 lines (67 loc) · 2.4 KB
/
script.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
//Initial References
let result = document.getElementById("result");
let searchBtn = document.getElementById("search-btn");
let url = "https://www.themealdb.com/api/json/v1/1/search.php?s=";
searchBtn.addEventListener("click", () => {
let userInp = document.getElementById("user-inp").value;
if (userInp.length == 0) {
result.innerHTML = `<h3>Input Field Cannot Be Empty</h3>`;
} else {
fetch(url + userInp)
.then((response) => response.json())
.then((data) => {
let myMeal = data.meals[0];
console.log(myMeal);
console.log(myMeal.strMealThumb);
console.log(myMeal.strMeal);
console.log(myMeal.strArea);
console.log(myMeal.strInstructions);
let count = 1;
let ingredients = [];
for (let i in myMeal) {
let ingredient = "";
let measure = "";
if (i.startsWith("strIngredient") && myMeal[i]) {
ingredient = myMeal[i];
measure = myMeal[`strMeasure` + count];
count += 1;
ingredients.push(`${measure} ${ingredient}`);
}
}
console.log(ingredients);
result.innerHTML = `
<img src=${myMeal.strMealThumb}>
<div class="details">
<h2>${myMeal.strMeal}</h2>
<h4>${myMeal.strArea}</h4>
</div>
<div id="ingredient-con"></div>
<div id="recipe">
<button id="hide-recipe">X</button>
<pre id="instructions">${myMeal.strInstructions}</pre>
</div>
<button id="show-recipe">View Recipe</button>
`;
let ingredientCon = document.getElementById("ingredient-con");
let parent = document.createElement("ul");
let recipe = document.getElementById("recipe");
let hideRecipe = document.getElementById("hide-recipe");
let showRecipe = document.getElementById("show-recipe");
ingredients.forEach((i) => {
let child = document.createElement("li");
child.innerText = i;
parent.appendChild(child);
ingredientCon.appendChild(parent);
});
hideRecipe.addEventListener("click", () => {
recipe.style.display = "none";
});
showRecipe.addEventListener("click", () => {
recipe.style.display = "block";
});
})
.catch(() => {
result.innerHTML = `<h3>Invalid Input</h3>`;
});
}
});