-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (40 loc) · 1.58 KB
/
script.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
const API_KEY = "92f0263949a74da9877e2fb48b92fbf9";
const recipeListEl = document.getElementById("recipe-list");
function displayRecipes(recipes) {
recipeListEl.innerHTML = "";
recipes.forEach((recipe) => {
const recipeItemEl = document.createElement("li");
recipeItemEl.classList.add("recipe-item");
recipeImageEl = document.createElement("img");
recipeImageEl.src = recipe.image;
recipeImageEl.alt = "recipe image";
recipeTitleEl = document.createElement("h2");
recipeTitleEl.innerText = recipe.title;
recipeIngredientsEl = document.createElement("p");
recipeIngredientsEl.innerHTML = `
<strong>Ingredients:</strong> ${recipe.extendedIngredients
.map((ingredient) => ingredient.original)
.join(", ")}
`;
recipeLinkEl = document.createElement("a");
recipeLinkEl.href = recipe.sourceUrl;
recipeLinkEl.innerText = "View Recipe";
recipeItemEl.appendChild(recipeImageEl);
recipeItemEl.appendChild(recipeTitleEl);
recipeItemEl.appendChild(recipeIngredientsEl);
recipeItemEl.appendChild(recipeLinkEl);
recipeListEl.appendChild(recipeItemEl);
});
}
async function getRecipes() {
const response = await fetch(
`https://api.spoonacular.com/recipes/random?number=6&apiKey=${API_KEY}`
);
const data = await response.json();
return data.recipes;
}
async function init() {
const recipes = await getRecipes();
displayRecipes(recipes);
}
init();