-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.js
55 lines (50 loc) · 1.59 KB
/
recipe.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
const random = document.getElementById('random'),
mealsEl = document.getElementById('meals'),
resultHeading = document.getElementById('result-heading'),
single_mealEl = document.getElementById('single-meal');
getRandomMeal();
function getRandomMeal() {
//Clear meals and heading
mealsEl.innerHTML = '';
resultHeading.innerHTML = '';
fetch(`https://www.themealdb.com/api/json/v1/1/random.php`)
.then(res => res.json())
.then(data => {
console.log(data);
const meal = data.meals[0];
addMealToDom(meal);
});
}
function addMealToDom(meal) {
const ingredients = [];
for (let i = 1; i <= 20; i++) {
if (meal[`strIngredient${i}`]) {
ingredients.push(
`${meal[`strIngredient${i}`]} - ${meal[`strMeasure${i}`]}`
);
} else {
break;
}
}
single_mealEl.innerHTML = `
<div class="single-meal">
<u><i><h1>${meal.strMeal}</h1></i></u>
<img src="${meal.strMealThumb}" alt="" />
<div class="single-meal-info">
<h2><b>${meal.strCategory}</b></h2>
</div>
<div class="main">
<p>${meal.strInstructions}</p>
<h2>Ingredients</h2>
<ul>
${ingredients.map(ing => `<li>${ing}</li>`).join('')}
</ul>
<a href='${meal.strSource}'><h3>Source</h3></a>
<a href='${meal.strYoutube}'><h3>Find This on YouTube</h3></a>
</div>
</div>
`;
}
random.addEventListener('click', () => {
window.location.href = './recipe.html';
});