javascript计算不工作一直返回NaN

时间:2017-05-25 05:36:37

标签: javascript html nan

需要使用警告对话框帮助返回整个销售成本和零售标记变量的计算值。它显示为NaN。

<!DOCTYPE html>
        <html>
            <head>

            </head>
            <body>
                <button onclick="myFunction();">Try it</button>
                <p id="demo"></p>

                <script>
                    // Create an application that lets the user enter an item’s 
                       wholesale cost and its markup percentage.
                        function myFunction() {
                        var wholeSaleCost;
                        var markUpPercentage;
                        var retailPrice;

                        wholeSaleCanDollars = prompt("Please enter the wholesale cost of the item:");
                        markupCostCanDollars = prompt("Please enter the markup cost for the same item:");

                        retailPrice = calculateRetail(wholeSaleCost, markUpPercentage);
                        alert(retailPrice);
                        document.getElementById("demo").innerHTML = txt;
                    }

                    function calculateRetail(wholeSale, markUp) {
                        var markUpConverted = markUp / 100;
                        var markUpAmount = (wholeSale * markUpConverted);
                        var retail = (wholeSale + markUpAmount);
                        return retail;
                    }`enter code here`

                </script>
            </body>
        </html>

2 个答案:

答案 0 :(得分:1)

在您的特定情况下,您无需将输入转换为数字,因为除法等操作会自动将它们转换为数字(仍然,作为一种良好的做法,为了保持一致性,您应始终将字符串输入转换为数字)。

错误是您已将输入存储在某些变量中,并将其他一些变量(undefined)传递给您的函数。纠正这个问题,你的代码运行正常:

&#13;
&#13;
                    // Create an application that lets the user enter an item’s                        wholesale cost and its markup percentage.
                        function myFunction() {
                        var wholeSaleCost;
                        var markUpPercentage;
                        var retailPrice;

                        wholeSaleCost = prompt("Please enter the wholesale cost of the item:");
                        markUpPercentage = prompt("Please enter the markup cost for the same item:");

                        retailPrice = calculateRetail(wholeSaleCost, markUpPercentage);
                        alert(retailPrice);
                        document.getElementById("demo").innerHTML = txt;
                    }

                    function calculateRetail(wholeSale, markUp) {
                        var markUpConverted = markUp / 100;
                        var markUpAmount = (wholeSale * markUpConverted);
                        var retail = (wholeSale + markUpAmount);
                        return retail;
                    }
&#13;
                <button onclick="myFunction();">Try it</button>
                <p id="demo"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用parseInt or parseFloat将费用输入更改为数字。使用以下命令更新脚本。

 retailPrice = calculateRetail(parseInt(wholeSaleCost), parseInt(markUpPercentage));