-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (44 loc) · 1.53 KB
/
script.js
File metadata and controls
59 lines (44 loc) · 1.53 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
52
53
54
55
56
57
58
let year = new Date().getFullYear();
document.getElementById("year").textContent = year;
const container = document.querySelector(".container");
const ingredients = document.querySelector(".ingredients");
const btn = document.querySelector(".btn");
const receipeResult = document.querySelector(".receipeResult");
const apikey = "213649eff1814fabb438d83f1a0f47a6";
// need 2 function
// first to fetch data
//latter to display card on ui
btn.addEventListener("click" , ()=> {
const ingredientInput = ingredients.value.trim();
fetchReceipes(ingredientInput);
});
const fetchReceipes = async(ingredientInput) => {
try {
const response = await fetch(
`https://api.spoonacular.com/recipes/findByIngredients?ingredients=${ingredientInput}&number=10&apiKey=${apikey}`
);
const receipes = await response.json();
displayReceipes(receipes);
} catch (error) {
console.log(error);
}
};
const displayReceipes = (receipes)=> {
receipeResult.innerHTML = "";
if(receipes.length === 0) {
alert("Recipe ghosted! Try writing valid ingredient.")
return
}
receipes.forEach((receipe)=> {
const receipeCard = document.createElement("div");
receipeCard.classList.add('card')
receipeCard.innerHTML = `
<div class="img">
<img src="${receipe.image}" alt="" />
</div>
<p>${receipe.title}</p>
<button class="btn"> Save to Favourite</button>
`;
receipeResult.appendChild(receipeCard);
});
};