gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:15:10 27 4
gpt4 key购买 nike

需要帮助使用警报对话框返回整个销售成本和零售加价变量的计算值。它显示为 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/

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