-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcook.js
62 lines (55 loc) · 2.42 KB
/
cook.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
51
52
53
54
55
56
57
58
59
60
61
62
// cook.js
// Key and values for filter buttons
const obj = {
"china-btn": "chinese",
"italy-btn": "italian",
"usa-btn": "american",
"all-btn": "all",
"asia-btn": "asian",
"popular-btn": "popular",
"vegetarian-btn": "vegetarian",
"hightolowbtn": "hightolow",
"sort-price-btn": "sort-price",
"random-btn": "random"
};
Object.keys(obj).forEach(buttonId => {
const button = document.getElementById(buttonId);
button.addEventListener("click", () => {
const targetElementId = obj[buttonId];
console.log(`Filtering for: ${targetElementId}`);
document.querySelectorAll(".card").forEach(card => {
const cuisines = card.getAttribute("data-cuisines")?.toLowerCase().split(",") || []; // Store cuisines in a data attribute
const isVegetarian = card.classList.contains("vegetarian");
const isVeryPopular = card.classList.contains("very-popular");
// Show all recipes
if (targetElementId === "all") {
card.style.display = "block";
}
// Show vegetarian recipes
else if (targetElementId === "vegetarian" && isVegetarian) {
card.style.display = "block";
}
// Show recipes that match cuisine
else if (cuisines.includes(targetElementId.toLowerCase())) {
card.style.display = "block";
}
// Show very popular recipes
else if (targetElementId === "popular" && isVeryPopular) {
card.style.display = "block";
}
// Hide all other cards
else {
card.style.display = "none";
}
});
});
});
// Example clock function to display the current time in the console. not being used right now
const clock = () => {
let currentTime = new Date(); //to get the current time
let hrs = (currentTime.getHours() < 10 ? "0" : "") + currentTime.getHours(); //to display hours and give double digit output
let mins = (currentTime.getMinutes() < 10 ? "0" : "") + currentTime.getMinutes(); //to display minutes and give double digit output
let sec = (currentTime.getSeconds() < 10 ? "0" : "") + currentTime.getSeconds(); //to display seconds and give double digit output
console.clear(); //clear the console to continuously update the time
console.log(hrs +':'+ mins +':'+ sec); //display the current time
};