-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (39 loc) · 1.51 KB
/
index.js
File metadata and controls
48 lines (39 loc) · 1.51 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
"use strict"
const opti = () => {
const getFormData = () => {
const formData = new FormData(document.getElementById("optifast-calculator"))
return {
carbohydrates: parseFloat(formData.get("carbohydrates")),
fat: parseFloat(formData.get("fat")),
protein: parseFloat(formData.get("protein")),
mixed: formData.get("mixed")
}
}
const calculateGreenPoints = (carbs) => +carbs / 10
const calculateYellowOrRed = (isYellow) => (fat) => isYellow ? { yellow: +fat } : { red: +fat }
const calculateYellowAndRed = (fat) => {
const red = Math.floor(fat / 3)
const yellow = fat - red
return {yellow, red}
}
const printResult = ({green, yellow, red}) => {
document.getElementById("green-result").innerHTML = green ? green : ""
document.getElementById("red-result").innerHTML = red ? red : ""
document.getElementById("yellow-result").innerHTML = yellow ? yellow : ""
}
const handleFormData = (event) => {
event.preventDefault()
const { carbohydrates, fat, protein, mixed } = getFormData()
const isYellow = !mixed && protein > fat
const calculateFatPoints = mixed ? calculateYellowAndRed : calculateYellowOrRed(isYellow)
const { yellow, red } = calculateFatPoints(fat)
const green = calculateGreenPoints(carbohydrates)
printResult({green, yellow, red})
}
const addListeners = (callback) => {
document
.getElementById("optifast-calculator")
.addEventListener("submit", callback);
}
return {addListeners, handleFormData}
}