-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (41 loc) · 1.75 KB
/
Copy pathapp.js
File metadata and controls
51 lines (41 loc) · 1.75 KB
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
const API_KEY = "2b17214c61844b2681a5c27e3cbfbf61";
const recipeListElement = document.getElementById("recipe-list");
displayRecipes = (recipes) => {
recipeListElement.innerHTML = "";
recipes.forEach((recipe) => {
const recipeItemElement = document.createElement("li");
recipeItemElement.classList.add("recipe-item");
const recipeImageElement = document.createElement("img");
recipeImageElement.src = recipe.image;
recipeItemElement.alt = "recipe image";
const recipeTitleElement = document.createElement("h2");
recipeTitleElement.textContent = recipe.title;
const recipeIngredientesElement = document.createElement("p");
recipeIngredientesElement.innerHTML = `
<span><strong>Ingredientes:</strong> ${recipe.extendedIngredients
.map((ingredient) => ingredient.original)
.join(", ")}
</span>
`
const recipeLinkElement = document.createElement("a");
recipeLinkElement.href = recipe.sourceUrl;
recipeLinkElement.textContent = "Ver Receta";
recipeItemElement.appendChild(recipeImageElement);
recipeItemElement.appendChild(recipeTitleElement);
recipeItemElement.appendChild(recipeIngredientesElement);
recipeItemElement.appendChild(recipeLinkElement);
recipeListElement.appendChild(recipeItemElement);
});
}
async function getRecipes() {
const response = await fetch(
`https://api.spoonacular.com/recipes/random?number=10&apiKey=${API_KEY}`
);
const data = await response.json();
return data.recipes;
}
async function init() {
const recipes = await getRecipes();
displayRecipes(recipes);
}
init();