|
1 | 1 | const API_KEY = "275d58779ccf4e22af03e792e8819fff";
|
| 2 | +const recipeListEl = document.getElementById("recipe-list"); |
2 | 3 |
|
3 |
| -// Call the API and retrieve a list of recipes |
4 |
| -const recipeList = document.getElementById("recipe-list"); |
| 4 | +function displayRecipes(recipes) { |
| 5 | + recipeListEl.innerHTML = ""; |
| 6 | + recipes.forEach((recipe) => { |
| 7 | + const recipeItemEl = document.createElement("li"); |
| 8 | + recipeItemEl.classList.add("recipe-item"); |
| 9 | + recipeImageEl = document.createElement("img"); |
| 10 | + recipeImageEl.src = recipe.image; |
| 11 | + recipeImageEl.alt = "recipe image"; |
| 12 | + |
| 13 | + recipeTitleEl = document.createElement("h2"); |
| 14 | + recipeTitleEl.innerText = recipe.title; |
| 15 | + |
| 16 | + recipeIngredientsEl = document.createElement("p"); |
| 17 | + recipeIngredientsEl.innerHTML = ` |
| 18 | + <strong>Ingredients:</strong> ${recipe.extendedIngredients |
| 19 | + .map((ingredient) => ingredient.original) |
| 20 | + .join(", ")} |
| 21 | + `; |
| 22 | + |
| 23 | + recipeLinkEl = document.createElement("a"); |
| 24 | + recipeLinkEl.href = recipe.sourceUrl; |
| 25 | + recipeLinkEl.innerText = "View Recipe"; |
| 26 | + |
| 27 | + recipeItemEl.appendChild(recipeImageEl); |
| 28 | + recipeItemEl.appendChild(recipeTitleEl); |
| 29 | + recipeItemEl.appendChild(recipeIngredientsEl); |
| 30 | + recipeItemEl.appendChild(recipeLinkEl); |
| 31 | + recipeListEl.appendChild(recipeItemEl); |
| 32 | + }); |
| 33 | +} |
5 | 34 |
|
6 | 35 | async function getRecipes() {
|
7 | 36 | const response = await fetch(
|
8 | 37 | `https://api.spoonacular.com/recipes/random?number=10&apiKey=${API_KEY}`
|
9 | 38 | );
|
10 |
| - const data = await response.json(); |
11 |
| - return data.recipes; |
12 |
| -} |
13 |
| - |
14 |
| -function displayRecipes(recipes) { |
15 |
| - recipeList.innerHTML = ""; |
16 |
| - recipes.forEach((recipe) => { |
17 |
| - const recipeItem = document.createElement("li"); |
18 |
| - recipeItem.classList.add("recipe-item"); |
19 |
| - const recipeImage = document.createElement("img"); |
20 |
| - recipeImage.src = recipe.image; |
21 |
| - |
22 |
| - const recipeTitle = document.createElement("h2"); |
23 |
| - recipeTitle.innerText = recipe.title; |
24 |
| - |
25 |
| - const recipeIngredients = document.createElement("p"); |
26 |
| - recipeIngredients.innerHTML = `<strong>Ingredients:</strong> ${recipe.extendedIngredients |
27 |
| - .map((ingredient) => ingredient.original) |
28 |
| - .join(", ")}`; |
29 | 39 |
|
30 |
| - const recipeLink = document.createElement("a"); |
31 |
| - recipeLink.href = recipe.sourceUrl; |
32 |
| - recipeLink.innerText = "View Recipe"; |
33 |
| - |
34 |
| - recipeItem.appendChild(recipeImage); |
35 |
| - recipeItem.appendChild(recipeTitle); |
36 |
| - recipeItem.appendChild(recipeIngredients); |
37 |
| - recipeItem.appendChild(recipeLink); |
| 40 | + const data = await response.json(); |
38 | 41 |
|
39 |
| - recipeList.appendChild(recipeItem); |
40 |
| - }); |
| 42 | + return data.recipes; |
41 | 43 | }
|
42 | 44 |
|
43 | 45 | async function init() {
|
|
0 commit comments