我目前的解决方案并未计算增加的船舶成本。我的情况不起作用,我已经能够用功能做类似的问题,但我想知道我在哪里出错了。
<html>
<script type="text/javascript">
var numOrdered = 0;
var priceItem = 0;
var shipCost = 0;
var amtDue = 0;
numOrdered = prompt("Enter the number ordered ",0);
priceItem = prompt("Enter the price of the item ",0);
amtDue = numOrdered * priceItem;
if (amtDue > 1000)
{
shipCost = 0;
}
else
{
shipCost = amtDue * .1;
}
amtDue = amtDue + shipCost;
document.write("You owe ", amtDue);
</script>
</html>
答案 0 :(得分:0)
我会查看Chrome或Firefox中的开发者工具,并使用console.log或添加一些断点来帮助您了解哪些有效,哪些无效。通过这种方式,您可以查看您的javascript或html文档是否失败。
话虽如此,您正在比较字符串而不是数字,因此您需要对从提示中返回的值使用parseInt。
priceItem = parseInt(priceItem, 10);
答案 1 :(得分:0)
它对我有用。我把你的代码放在这里:http://jsfiddle.net/XVzKb/
无变化 - 如果我输入5然后再次提示10,则页面中显示的总数为55,即5 * 10 * 1.1 = 55.请注意,如果您的输入包含非数字字符,例如{{1或者$
然后你需要像这样解析它:
,