|
| 1 | +const API_KEY = "275d58779ccf4e22af03e792e8819fff"; |
| 2 | + |
| 3 | +// Call the API and retrieve a list of recipes |
| 4 | +const recipeList = document.getElementById("recipe-list"); |
| 5 | + |
| 6 | +async function getRecipes() { |
| 7 | + const response = await fetch( |
| 8 | + `https://api.spoonacular.com/recipes/random?number=10&apiKey=${API_KEY}` |
| 9 | + ); |
| 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 | + |
| 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); |
| 38 | + |
| 39 | + recipeList.appendChild(recipeItem); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +async function init() { |
| 44 | + const recipes = await getRecipes(); |
| 45 | + displayRecipes(recipes); |
| 46 | +} |
| 47 | + |
| 48 | +init(); |
0 commit comments