1
+ const API_KEY = "6716bbd7e07b44498e4287907d91a5ca" ;
1
2
3
+ const recipeListElements = document . getElementById ( 'recipe-list' ) ;
2
4
3
- const API_KEY = "6716bbd7e07b44498e4287907d91a5ca" ;
5
+ function displayRecipes ( recipes ) {
6
+ recipeListElements . innerHTML = "" ;
7
+ recipes . forEach ( ( recipe ) => {
8
+ const recipeItemEl = document . createElement ( "li" ) ;
9
+ recipeItemEl . classList . add ( "recipe-item" ) ;
10
+
11
+ const recipeImageEl = document . createElement ( "img" ) ;
12
+ recipeImageEl . src = recipe . image ;
13
+ recipeImageEl . alt = "recipe" ;
14
+
15
+ const recipeTitleEl = document . createElement ( "h2" ) ;
16
+ recipeTitleEl . innerText = recipe . title ;
17
+
18
+ recipeIngredientsEl = document . createElement ( "p" ) ;
19
+ recipeIngredientsEl . innerHTML = `
20
+ <strong>Ingredients:</strong> ${ recipe . extendedIngredients . map ( ( ingredient ) => ingredient . original ) . join ( ", " ) }
21
+ ` ;
22
+
23
+ const recipeLinkEl = document . createElement ( "a" ) ;
24
+ recipeLinkEl . href = recipe . sourceUrl ;
25
+ recipeLinkEl . innerText = "View Recipe" ;
26
+
27
+ recipeItemEl . appendChild ( recipeImageEl ) ;
28
+
29
+ recipeItemEl . appendChild ( recipeTitleEl ) ;
30
+
31
+ recipeItemEl . appendChild ( recipeIngredientsEl ) ;
32
+
33
+ recipeItemEl . appendChild ( recipeLinkEl ) ;
34
+
35
+ recipeListElements . appendChild ( recipeItemEl ) ;
36
+ } ) ;
37
+ }
4
38
5
39
async function getRecipes ( ) {
6
40
const response = await fetch ( `https://api.spoonacular.com/recipes/random?number=10&apiKey=${ API_KEY } ` )
@@ -13,7 +47,8 @@ async function getRecipes() {
13
47
async function init ( ) {
14
48
const recipes = await getRecipes ( )
15
49
16
- console . log ( recipes ) ;
50
+ // console.log(recipes);
51
+ displayRecipes ( recipes ) ;
17
52
}
18
53
19
54
init ( )
0 commit comments