generated from chingu-voyages/voyage-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
107 lines (79 loc) · 3.46 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const days = ["Mon.", "Tues.", "Wed.", "Thu.", "Fri.", "Sat.", "Sun."];
// use this function to generate random dishes, with ingredients
export function generateRandomDishes(userAlergies, dishes, numberOfDishes) {
// generate random dishes, without alergies
let currentDishCount = 0;
let randomDishes = {}; // {index: {dishDetail}}
let visitedDish = new Set(); // (0)
let dishesLength = dishes.length;
while (currentDishCount < numberOfDishes && visitedDish.size < dishesLength) {
// stop the loop 1) when we find the number of dishes we desire
// 2) when we have visited all the possible dishes in our set
let randomDishIndex = Math.floor(Math.random() * dishesLength);// 0
if (visitedDish.has(randomDishIndex)) {
continue;
}
visitedDish.add(randomDishIndex);
if (randomDishIndex in randomDishes) continue; // not duplicating dish
let randomDish = dishes[randomDishIndex];
// check if any of the food ingeredients are present in the user alergies choice
let foodHasAlergies = randomDish["ingredients"].some(ingredient => userAlergies.has(ingredient));
if (foodHasAlergies) {
console.log("Hash Alergi: " + foodHasAlergies + " Index: " + randomDishIndex);
console.log(randomDish);
continue;
}
// if dish is not in alergies list, and is unique, add
randomDishes[randomDishIndex] = randomDish;
currentDishCount += 1;
}
return Object.values(randomDishes); // ignore index of the dish and return dish detail
}
export function generateWeeklyFood(randomDishes, userPickedStartDate) {
const daysContainer = document.getElementById("days");
daysContainer.innerHTML = "";
let numberOfDays = 7; // Math.abs(userPickedEndDate - userPickedStartDate);
for (let startDay = userPickedStartDate; startDay < (userPickedStartDate + numberOfDays); startDay++) {
let index = startDay % 7;
daysContainer.appendChild(createWeekdayStructure(days[index], index, randomDishes[index]));
}
}
export function createWeekdayStructure(day, index, dish) {
// Create the parent list item element
const li = document.createElement('li');
li.className = 'weekday';
li.id = 'div' + (index + 1);
// Create the first child div with Sunday heading
const boxDiv = document.createElement('div');
boxDiv.className = `box ${day.toLowerCase()}`;
const h3 = document.createElement('h3');
h3.className = 'h3';
h3.textContent = day;
boxDiv.appendChild(h3);
li.appendChild(boxDiv);
// Create the bubble div
const bubbleDiv = document.createElement('div');
bubbleDiv.className = 'bubble';
bubbleDiv.id = day;
// Create the foodinfo child divs
const dishName = document.createElement('div');
dishName.className = 'foodinfo';
dishName.id = 'dayname';
dishName.innerText = dish.name;
const dishIngredients = document.createElement('div');
dishIngredients.className = 'foodinfo';
dishIngredients.id = 'dayingredients';
dishIngredients.innerText = dish.ingredients.join(", ");
const dishCalories = document.createElement('div');
dishCalories.className = 'foodinfo';
dishCalories.id = 'daycalories';
dishCalories.innerText = dish.calories + " Calories";
// Append the foodinfo divs to the bubble div
bubbleDiv.appendChild(dishName);
bubbleDiv.appendChild(dishIngredients);
bubbleDiv.appendChild(dishCalories);
// Append the bubble div to the list item
li.appendChild(bubbleDiv);
// Append the entire structure to the document (e.g., to a parent ul)
return li;
}