-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
120 lines (98 loc) · 4.1 KB
/
script.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
108
109
110
111
112
113
114
115
116
117
118
119
120
const calculateDoughRecipe = (finalMass = 750, hydration = 72, flour1Percent = 80,
flour2Percent = 20, /*flour3Percent = 0,*/ saltPercent = 2.5,
starterPercent = 16) => {
// Calculate the mass of sourdough starter
const starterMass = finalMass * starterPercent / 100
// Calculate the total flour mass
const flourMass = (finalMass - starterMass * (saltPercent / 100 + hydration / 100 + 1) / 2) / (saltPercent / 100 + hydration / 100 + 1)
// Calculate the total water mass
const waterMass = flourMass * hydration / 100 + starterMass * (hydration / 100 - 1) / 2
// Calculate the mass of each flour type
const flour1Mass = flourMass * flour1Percent / 100
const flour2Mass = flourMass * flour2Percent / 100
// const flour3Mass = flourMass * flour3Percent / 100
// Calculate the total flour mass
const flourTotal = flour1Mass + flour2Mass //+ flour3Mass
// Calculate the mass of salt
const saltMass = (flourTotal + starterMass / 2) * saltPercent / 100
// Return the recipe
return {
dateTime: new Date().toLocaleString(),
flour1Mass,
flour2Mass,
// flour3Mass,
flourTotal,
waterMass,
saltMass,
starterMass,
}
}
document.querySelector("#calculate-recipe").addEventListener("click", () => {
// const recipe = calculateDoughRecipe()
const table = document.querySelector("#results tbody")
const row = document.createElement("tr")
// get the input values from the table to pass to the function
const finalMass = document.getElementById("final-mass").value;
const hydration = document.getElementById("hydration").value;
const flour1Percent = document.getElementById("flour1-percent").value;
const flour2Percent = document.getElementById("flour2-percent").value;
// const flour3Percent = document.getElementById("flour3-percent").value;
const saltPercent = document.getElementById("salt-percent").value;
const starterPercent = document.getElementById("starter-percent").value;
// pass the input values to the function to calculate the recipe
const recipe = calculateDoughRecipe(finalMass, hydration, flour1Percent, flour2Percent, /*flour3Percent,*/ saltPercent, starterPercent)
row.innerHTML = `
<td>${recipe.dateTime}</td>
<td>${recipe.flour1Mass.toFixed(2)}</td>
<td>${recipe.flour2Mass.toFixed(2)}</td>
<td>${recipe.flourTotal.toFixed(2)}</td>
<td>${recipe.waterMass.toFixed(2)}</td>
<td>${recipe.saltMass.toFixed(2)}</td>
<td>${recipe.starterMass.toFixed(2)}</td>
`
// table.appendChild(row)
table.insertBefore(row, table.firstChild)
})
// make a listener to press the calculate recipe button when the user presses enter
document.getElementById("final-mass").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});
document.getElementById("hydration").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});
document.getElementById("flour1-percent").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});
document.getElementById("flour2-percent").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});
/*document.getElementById("flour3-percent").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});*/
document.getElementById("salt-percent").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});
document.getElementById("starter-percent").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("calculate-recipe").click();
}
});