gpt4 book ai didi

javascript - 公式返回的值略低于 "correct"答案?

转载 作者:行者123 更新时间:2023-11-28 10:19:20 28 4
gpt4 key购买 nike

好吧,我距离数学高手还很远。哎呀,我记住了足够多的高中代数,可以拼凑出任何有效的公式,这对我来说是一个胜利。因此,如果您注意到这里有一个不必要的长或令人困惑的公式,那就可以解释了。

但是,正如人们可以合理预期的那样,这里出了问题。

我正在尝试制作一个简单的计算器,它可以显示购买交通车票与支付现金票价相比所节省的成本。我已经非常接近了,但是我使用的公式的计算在某些情况下无法正常工作,我不明白为什么。

在下面的示例中,基本区域和区域 4 按我的预期计算差异,而区域 1-3 返回的答案仅比正确答案低一点点(即 $105.60000000000014 而不是 $106),区域 1 和区域 2 返回尽管公式不同,但答案相同。

我一直看着这个,直到我变得斗鸡眼。我确信答案很简单,但我看不到。有人吗?

感谢您的帮助。

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<script language="javascript">

<!-- Begin Trip Tickets Savings Calc script
function doMath4() {
var one = parseInt(document.theForm4.elements[0].value);
var two = parseInt(document.theForm4.elements[1].value);
var selection = document.getElementsByName("zonett")[0].value;

if(selection == "z4"){
var prodZ4tt = (((one * two) * 4.25) *12) - (((one * two) * 3.75) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ4tt + ".");
}
else if(selection == "z3"){
var prodZ3tt = (((one * two) * 3.75) *12) - (((one * two) * 3.35) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ3tt + ".");
}
else if(selection == "z2"){
var prodZ2tt = (((one * two) * 3) *12) - (((one * two) * 2.8) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ2tt + ".");
}
else if(selection == "z1"){
var prodZ1tt = (((one * two) * 2.5) *12) - (((one * two) * 2.3) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ1tt + ".");
}
else if(selection == "Base"){
var prodBasett = (((one * two) * 1.5) *12) - (((one * two) * 1.5) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodBasett + ".");
}
}

// End Trip Tickets Savings Calc script -->

</script>
</head>

<body>


<form name="theForm4" class="calcform">
<h2>You Do the Math: Commuter Express Trip Tickets Vs. Cash</h2>
<div class="calcform-content">
<div class="formrow-calc">
<div class="calcform-col1">
<p>Days you commute on Commuter Express monthly:</p>
</div>
<div class="calcform-col2">
<input type="text">
</div>
<div class="calcform-col3">&nbsp;</div>
</div>
<div class="clear"></div>


<div class="formrow-calc">
<div class="calcform-col1">
<p>Daily boardings on Commuter Express Bus:</p>

<table border="0" cellspacing="0" cellpadding="0" class="fareexampletable">
<tr>
<td colspan="2" class="savingsleft"><p class="ifyouride">EXAMPLE:</p></td>
</tr>
<tr>
<td class="savingsleft"><p><strong>Go to work:</strong></p></td>
<td class="savingsright"><p>1 time</p></td>
</tr>
<tr>
<td class="savingsleft"><p><strong>Come home from work:</strong></p></td>
<td class="additionline savingsright"><p>+1 time</p></td>
</tr>
<tr>
<td class="savingsleft"><p><strong>Total:</strong></p></td>
<td class="savingsright"><p>2 times</p></td>
</tr>
</table>
</div>
<div class="calcform-col2">
<input type="text">
</div>
<div class="calcform-col3">&nbsp;</div>
</div>
<div class="clear"></div>


<div class="formrow-calc savings-zone">
<div class="calcform-col1">
<p>Choose Zone:</p>
</div>
<div class="calcform-col2">
<select name="zonett">
<option value="Base">Base</option>
<option value="z1">Zone 1</option>
<option value="z2">Zone 2</option>
<option value="z3">Zone 3</option>
<option value="z4">Zone 4</option>
</select>
</div>
</div>



<div class="formrow-calc">


<div class="calcform-col4-ce">


<button type="submit" onclick="doMath4()" class="btn-submit"><div class="btn-submit"><img src="img/btn_savings.png" alt="Show My Yearly Savings" /></div></button>


</div>
</div>
<div class="clear">


</div>



</div>
</form>

</body>
</html>

最佳答案

这是我在评论中讨论的清理后的代码:

function  doMath4() {
var one = parseInt(document.theForm4.elements[0].value);
var two = parseInt(document.theForm4.elements[1].value);
var selection = document.getElementsByName("zonett")[0].value;

var m12 = one * two * 12;
var prod = null;

if(selection == "z4")
{
prod = (m12 * 4.25) - (m12 * 3.75);
}
else if(selection == "z3")
{
prod = (m12 * 3.75) - (m12 * 3.35);
}
else if(selection == "z2")
{
prod = (m12 * 3) - (m12 * 2.8);
}
else if(selection == "z1")
{
prod = (m12 * 2.5) - (m12 * 2.32);
}
else if(selection == "Base")
{
prod = (m12 * 1.5) - (m12 * 1.5); //This is always 0?
}

if(prod != null)
{
prod = Math.round(prod);
alert("Your yearly savings if you buy Trip Tickets is $" + prod + ".");
}
}

上面(和下面)包含了您问题的最简单解决方案:

prod = Math.round(prod);

这会将结果 (prod) 四舍五入到最接近的整数,无论​​它是什么。因此,在您的示例中,$105.600000...将四舍五入为 $106,这就是您所要求的。

关于javascript - 公式返回的值略低于 "correct"答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942961/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com