-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
193 lines (175 loc) · 6.69 KB
/
index.html
File metadata and controls
193 lines (175 loc) · 6.69 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<html>
<head>
<title>碳循环计算器</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<style>
*, *::before, *::after {
box-sizing: border-box;
}
#form {
max-width: 480px;
margin: 0 auto;
margin-bottom: 20px;
}
.row {
margin: 5px 0;
}
.row label {
display: inline-block;
min-width: 150px;
}
#submit {
margin-top: 40px;
}
#output {
max-width: 480px;
margin: 0 auto;
line-height: 1.5;
}
.info-title, .info-value {
display: inline-block;
}
.info-title {
min-width: 10em;
}
.info-value {
min-width: 5em;
text-align: right;
}
</style>
</head>
<body>
<div id="form">
<div class="row">
<label for="height">身高(cm)</label><input id="height" type="number" value="170">
</div>
<div class="row">
<label for="weight">体重(kg)</label><input id="weight" type="number" value="70">
</div>
<div class="row">
<label for="age">年龄</label><input id="age" type="number" value="25">
</div>
<div class="row">
<label for="sex">女性</label><input id="sex" type="checkbox" checked>
</div>
<div class="row">
<label for="gap">预期热量缺口</label><input id="gap" type="number" value="400">
</div>
<div class="row">
<button id="submit">提交</button>
</div>
</div>
<div id="output"></div>
<script>
window.addEventListener('load', function () {
const $weight = qs('#weight')
const $height = qs('#height')
const $age = qs('#age')
const $sex = qs('#sex')
const $gap = qs('#gap')
const $output = qs('#output')
qs('#submit').addEventListener('click', function () {
const weight = $weight.value
const height = $height.value
const age = $age.value
const sex = $sex.value
const gap = $gap.value
// need for every kg
const protein = sex ? 2 : 3
const kcalMap = {
carbon: 4,
protein: 4,
fat: 9
}
let res
if (sex) {
res = 655 + (9.6 * weight) + (1.8 * height) - (4.7 * age)
} else {
res = 66 + (13.7 * weight) + (5 * height) - (6.8 * age)
}
const consume = res * 1.55
const intake = consume - gap - 200 /* 蔬菜 */
const highStr = 2
const lowStr = 1.8
const lowCarbon = 1
const highCarbon = 3
// 低碳日碳水 g
const lowDayCarbon = weight * lowCarbon
// 高碳日碳水 g
const highDayCarbon = weight * highCarbon
// 低强度蛋白质 g
const lowStrDayProtein = weight * lowStr
// 高强度蛋白质 g
const highStrDayProtein = weight * highStr
// 低强度低碳日脂肪 g
const lowDayLowStrFat = (intake - lowDayCarbon * kcalMap.carbon - lowStrDayProtein * kcalMap.protein) / kcalMap.fat
// 低强度高碳日脂肪 g
const lowDayHighStrFat = (intake - highDayCarbon * kcalMap.carbon - lowStrDayProtein * kcalMap.protein) / kcalMap.fat
// 高强度低碳日脂肪 g
const highDayLowStrFat = (intake - lowDayCarbon * kcalMap.carbon - highStrDayProtein * kcalMap.protein) / kcalMap.fat
// 高强度高碳日脂肪 g
const highDayHighStrFat = (intake - highDayCarbon * kcalMap.carbon - highStrDayProtein * kcalMap.protein) / kcalMap.fat
$output.innerHTML = `
<div class="info-row">
<div class="info-title">每日消耗热量</div>
<div class="info-value">${fixNumber(consume)}kcal</div>
</div>
<div class="info-row">
<div class="info-title">每日摄入热量</div>
<div class="info-value">${fixNumber(intake)}kcal</div>
</div>
<div class="info-row">
<div class="info-title">低强度蛋白摄入</div>
<div class="info-value">${fixNumber(lowStrDayProtein * kcalMap.protein)}kcal</div>
<div class="info-value">${fixNumber(lowStrDayProtein)}g</div>
</div>
<div class="info-row">
<div class="info-title">高强度蛋白摄入</div>
<div class="info-value">${fixNumber(highStrDayProtein * kcalMap.protein)}kcal</div>
<div class="info-value">${fixNumber(highStrDayProtein)}g</div>
</div>
<div class="info-row">
<div class="info-title">低碳日碳水摄入</div>
<div class="info-value">${fixNumber(lowDayCarbon * kcalMap.carbon)}kcal</div>
<div class="info-value">${fixNumber(lowDayCarbon)}g</div>
</div>
<div class="info-row">
<div class="info-title">高碳日碳水摄入</div>
<div class="info-value">${fixNumber(highDayCarbon * kcalMap.carbon)}kcal</div>
<div class="info-value">${fixNumber(highDayCarbon)}g</div>
</div>
<div class="info-row">
<div class="info-title">低强度低碳日脂肪摄入</div>
<div class="info-value">${fixNumber(lowDayLowStrFat * kcalMap.fat)}kcal</div>
<div class="info-value">${fixNumber(lowDayLowStrFat)}g</div>
</div>
<div class="info-row">
<div class="info-title">低强度高碳日脂肪摄入</div>
<div class="info-value">${fixNumber(lowDayHighStrFat * kcalMap.fat)}kcal</div>
<div class="info-value">${fixNumber(lowDayHighStrFat)}g</div>
</div>
<div class="info-row">
<div class="info-title">高强度低碳日脂肪摄入</div>
<div class="info-value">${fixNumber(highDayLowStrFat * kcalMap.fat)}kcal</div>
<div class="info-value">${fixNumber(highDayLowStrFat)}g</div>
</div>
<div class="info-row">
<div class="info-title">高强度高碳日脂肪摄入</div>
<div class="info-value">${fixNumber(highDayHighStrFat * kcalMap.fat)}kcal</div>
<div class="info-value">${fixNumber(highDayHighStrFat)}g</div>
</div>
`
})
})
/**
* @param {string} selector
*/
function qs (selector) {
return document.querySelector(selector)
}
function fixNumber (num) {
return Number(num).toFixed(0)
}
</script>
</body>
</html>