-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
121 lines (98 loc) · 4.3 KB
/
script.js
File metadata and controls
121 lines (98 loc) · 4.3 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
document.addEventListener("DOMContentLoaded", function () {
const ingredientsInput = document.getElementById("ingredients");
const inputContainer = document.getElementById("inputContainer");
const submitButton = document.getElementById("submitButton");
const outputContainer = document.getElementById("output");
submitButton.addEventListener("click", function () {
handleIngredientsInput();
});
ingredientsInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
handleIngredientsInput();
}
});
const handleIngredientsInput = () => {
const targetIngredients = parseInt(ingredientsInput.value);
const li = new Map();
let totalIngredients = 0;
const askIngredients = () => {
const ingredientLabel = document.createElement("label");
ingredientLabel.textContent = "Enter Ingredient and quantity in grams:";
inputContainer.appendChild(ingredientLabel);
const ingredientInput = document.createElement("input");
ingredientInput.type = "text";
inputContainer.appendChild(ingredientInput);
const quantityInput = document.createElement("input");
quantityInput.type = "number";
inputContainer.appendChild(quantityInput);
const lineBreak = document.createElement("br");
inputContainer.appendChild(lineBreak);
const submitButton = document.createElement("button");
submitButton.textContent = "Add Ingredient";
inputContainer.appendChild(submitButton);
const handleAddIngredient = () => {
const ingredient = ingredientInput.value;
const quantity = parseInt(quantityInput.value);
li.set(ingredient, quantity);
totalIngredients++;
if (totalIngredients < targetIngredients) {
inputContainer.innerHTML = "";
askIngredients();
} else {
let sum = 0;
li.forEach((quantity) => {
sum += quantity;
});
const weightLabel = document.createElement("label");
weightLabel.textContent = "Enter the weight your recipe has to be:";
inputContainer.appendChild(weightLabel);
const weightInput = document.createElement("input");
weightInput.type = "number";
weightInput.id = "weightInput";
inputContainer.appendChild(weightInput);
const calculateButton = document.createElement("button");
calculateButton.textContent = "Calculate";
inputContainer.appendChild(calculateButton);
const calculateValues = () => {
const targetWeight = parseInt(document.getElementById("weightInput").value);
const ratio = Math.floor(targetWeight / sum);
const updatedValues = document.createElement("div");
updatedValues.textContent = "Here are the updated values:";
li.forEach((quantity, ingredient) => {
const ingredientLine = document.createElement("div");
const updatedQuantity = quantity * ratio;
ingredientLine.textContent = ingredient + ": " + updatedQuantity + " grams";
updatedValues.appendChild(ingredientLine);
});
outputContainer.innerHTML = "";
outputContainer.appendChild(updatedValues);
};
calculateButton.addEventListener("click", function (event) {
event.preventDefault();
calculateValues();
});
weightInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
calculateValues();
}
});
}
};
submitButton.addEventListener("click", function (event) {
event.preventDefault();
handleAddIngredient();
});
ingredientInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
handleAddIngredient();
}
});
};
inputContainer.innerHTML = "";
outputContainer.innerHTML = "";
askIngredients();
};
});