javascript 计算不起作用一直返回 NaN

标签 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>

最佳答案

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

错误是您将输入存储在某些变量中,并将其他一些变量(未定义)传递给您的函数。纠正这个问题,您的代码就可以正常工作:

                    // 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;
                    }
                <button onclick="myFunction();">Try it</button>
                <p id="demo"></p>

关于javascript 计算不起作用一直返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44173027/

相关文章:

javascript - 在IE中使用XMLHttpRequest读取本地文件

SignalR 中的 Javascript 语法

javascript - 如何使用 JavaScript 计算终值 (FV)

javascript - 播放随机音乐

nan - tensorflow NaN 错误?

javascript - 增加 svg 的加载器宽度

javascript - jquery通过单击图标下拉搜索框

javascript - 点击折叠div

java - 重新定义扫描仪的非数字识别?

时间:2019-03-09 标签:c++odeintoutput "not a number"