From 04889661a09bd17531468b1d025a30a5032fbcca Mon Sep 17 00:00:00 2001 From: class03m07 Date: Thu, 24 Sep 2026 02:40:00 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=B8=89=E5=A4=A7=E8=90=A5?= =?UTF-8?q?=E5=85=BB=E7=B4=A0=E8=A1=A8=E6=A0=BC=E6=94=B9=E7=94=A8=E6=98=BE?= =?UTF-8?q?=E5=BC=8F=E8=A1=8C=E5=87=BD=E6=95=B0=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本次决策:把原先用字符串拼接 + replace 生成表格行的写法,改为 macroRow / nutriRow 两个行函数,避免脆弱的字符串替换,便于后续加营养素。 --- index.html | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 7bc5d75..4c08dad 100644 --- a/index.html +++ b/index.html @@ -118,8 +118,12 @@ return '肥胖'; } - function row(a, b) { - return '' + a + '' + b + ''; + function macroRow(name, pct, grams) { + return '' + name + '' + pct + '' + grams + ' g'; + } + + function nutriRow(name, value) { + return '' + name + '' + value + ''; } function calculate() { @@ -151,18 +155,18 @@ document.getElementById('rTarget').textContent = Math.round(target); document.getElementById('macroBody').innerHTML = - row('碳水化合物', '55%' + carb + ' g').replace('55%', '15%' + protein + ' g') + - row('脂肪', '30%').replace('30%', '30%' + fat + ' g'); + macroRow('碳水化合物', '55%', carb) + + macroRow('蛋白质', '15%', protein) + + macroRow('脂肪', '30%', fat); var ref = REF[sex]; document.getElementById('nutriBody').innerHTML = - row('蛋白质', protein + ' g(优质蛋白占一半以上)') + - row('脂类(脂肪)', fat + ' g(烹调油 25–30 g,饱和脂肪<10%)') + - row('碳水化合物', carb + ' g(添加糖<25 g,膳食纤维 25–30 g)') + - row('维生素', ref.vit) + - row('矿物质(无机盐)', ref.min) + - row('水', ref.water); + nutriRow('蛋白质', protein + ' g(优质蛋白占一半以上)') + + nutriRow('脂类(脂肪)', fat + ' g(烹调油 25–30 g,饱和脂肪<10%)') + + nutriRow('碳水化合物', carb + ' g(添加糖<25 g,膳食纤维 25–30 g)') + + nutriRow('维生素', ref.vit) + + nutriRow('矿物质(无机盐)', ref.min) + + nutriRow('水', ref.water); document.getElementById('result').style.display = 'block'; }