Cocktail Recipe Generator
Suggested Cocktail Recipe:
No cocktail recipes match the ingredients you entered.
'; return; } // Display the fetched recipes data.drinks.forEach(drink => { // Get detailed recipe information for each cocktail fetch(`${API_URL}lookup.php?i=${drink.idDrink}`) .then(response => response.json()) .then(detailData => { const cocktail = detailData.drinks[0]; const recipeDiv = document.createElement('div'); recipeDiv.classList.add('recipe-item'); recipeDiv.innerHTML = `${cocktail.strDrink}
Ingredients: ${getIngredients(cocktail).join(", ")}
Instructions: ${cocktail.strInstructions}
`; cocktailRecipeDiv.appendChild(recipeDiv); }) .catch(error => console.error('Error fetching cocktail details:', error)); }); } catch (error) { cocktailRecipeDiv.innerHTML = 'Error fetching cocktail data. Please try again later.
'; } } // Function to extract ingredients from the cocktail object function getIngredients(cocktail) { const ingredients = []; for (let i = 1; i <= 15; i++) { if (cocktail[`strIngredient${i}`]) { ingredients.push(cocktail[`strIngredient${i}`]); } } return ingredients; } // Add event listener to the "Generate Recipe" button document.getElementById('generateRecipe').addEventListener('click', generateCocktailRecipe);