Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单javascript装运计算器问题_Javascript_Html - Fatal编程技术网

简单javascript装运计算器问题

简单javascript装运计算器问题,javascript,html,Javascript,Html,这是我的代码。这是一个非常简单的航运计算器。当我运行它时,只显示一个询问购买价格的消息框,然后它显示这个消息框。有人知道发生了什么吗 <!DOCTYPE html> <html lang="en"> <head> <title> Shipping Calculator</title> <script > var purchasePrice = window.prompt("Please enter the pr

这是我的代码。这是一个非常简单的航运计算器。当我运行它时,只显示一个询问购买价格的消息框,然后它显示这个消息框。有人知道发生了什么吗

<!DOCTYPE html>

<html lang="en">
 <head>
    <title> Shipping Calculator</title>

<script >
var purchasePrice = window.prompt("Please enter the price of your purchase to calculate your                shipping cost.");
 var shippingPrice;

function shippingMath(purchasePrice, shippingPrice) {
     if (purchasePrice <= 25) {
        shippingPrice = 1.50;
    }
     else {
        shippingPrice = (purchasePrice * '.10');
    }
    var totalCost = (purchasePrice + shippingPrice);
    return totalCost;

}

shippingMath(purchasePrice, shippingPrice);

document.writeln("Your total is " + totalCost); 
</script>

</head>

<body>
</body>
</html>
感谢您的帮助。谢谢这是学校的作业,所以这是我被允许做的唯一方式。一个简单的函数,没有数组或其他任何东西。谢谢

既然你说这是做家庭作业的,我就试着在不直接给你答案的情况下引导你找到答案

您需要考虑以下几点:

您是否在html中正确声明了javascript 页面上什么时候运行javascript 函数如何尝试显示答案 您是否在函数中使用了正确的对象字符串与数字
如果您回答了这4个问题,那么解决方案就会跳出来。

您可能在代码中发现的一个潜在问题涉及变量的作用域

考虑以下几点:

function x() {
  var y = 10;
  return y;
}
函数x将返回值10,变量y将无法在函数x之外引用

因此,以下代码将显示一个包含10的警报框:

z = x();
alert(z);
而以下代码将引发错误或出现意外行为:

z = x();
alert(y);

我的0.02$,document.writeln是非常无效的代码,请使用.appendChild或.innerHTML.well。非常感谢。我将writeln语句移动到函数中。它显示了它的一部分,但随后变量显示为一些奇怪的符号。你在函数中使用了正确的对象字符串与数字吗?你真正遇到的问题是第三个问题:“你的函数如何显示答案?”。document.writeln的效率非常低。阅读@NiLL建议的.appendChild或.innerHTML的用法。这是一个很好的资源不,我真的认为问题是4。其他的都是错误,但我相信4是关键。