gpt4 book ai didi

javascript - 尝试计算价格时出现 NaN

转载 作者:行者123 更新时间:2023-12-02 16:00:44 25 4
gpt4 key购买 nike

我是编程新手,正在尝试创建一个简单的数学脚本来确定用户选择的游戏的价格,并将其乘以他们希望预订游戏的天数。

它有效,但最终价格总是显示为“NaN”(我相信它代表“Not a Number”)。

任何帮助将不胜感激。

<html>

<body>

<p><strong>Game ID</strong><br>
<input type="number" name="gameID" id="gameID" placeholder="1-8">

<p><strong>Days you wish to reserve the game for</strong><br>
<input type="number" name="daysReserved" id="daysReserved" placeholder="1-5">

<br>

<button type="button" onclick="idToPrice();finalPriceCalculation();">Reserve!</button>

<p id="totalPriceOutput"></p>

<script>
function idToPrice() {

var id = document.getElementById("gameID").value;

if (id == 1) {
var gamePrice = 0.99;
} else if (id == 2) {
var gamePrice = 0.99;
} else if (id == 3) {
var gamePrice = 1.99;
} else if (id == 4) {
var gamePrice = 1.99;
} else if (id == 5) {
var gamePrice = 3.99;
} else if (id == 6) {
var gamePrice = 3.99;
} else if (id == 7) {
var gamePrice = 0.99;
} else if (id == 8) {
var gamePrice = 0.99;
} else {
document.getElementById("totalPriceOutput").innerHTML = "Error. Your final price could not be calculated because you selected an invalid game ID.";
}
}

function finalPriceCalculation() {
var daysInputted;
var gamePrice;
var finalPrice = daysInputted * gamePrice;

document.getElementById("totalPriceOutput").innerHTML = "Your final price is &pound;" + finalPrice + ".";
}
</script>

</body>

</html>

最佳答案

注意:您的代码中存在3 个问题。我已经更正了它们,并使用 switch case 语句修改了条件,以提高可读性。

1。在此代码中,您的 var daysInputtedvar gamePrice 都是本地

var daysInputted;
var gamePrice;
var finalPrice = daysInputted * gamePrice;

您可能会想,当您首先调用 idToPrice() 方法时,必须定义 gamePrice 。但事实并非如此。

因为当您在方法中使用 var gamePrice 时,gamePrice 就成为该方法的局部变量,并且无法在任何其他方法中访问。

因此,您需要在同一方法中定义这两个变量,或者在 idToPrice() 方法中将它们设置为全局变量。

2。您还需要将 daysInputted 定义为

var daysInputted = document.getElementById("daysReserved").value;

3。您还需要解析 document.getElementById("gameID").valueInteger

您的最终代码完全工作的代码将是

<body>

<p><strong>Game ID</strong><br>
<input type="number" name="gameID" id="gameID" placeholder="1-8">

<p><strong>Days you wish to reserve the game for</strong><br>
<input type="number" name="daysReserved" id="daysReserved" placeholder="1-5">

<br>

<button type="button" onclick="idToPrice();finalPriceCalculation();">Reserve!</button>

<p id="totalPriceOutput"></p>

<script>
function idToPrice() {

var id = parseInt(document.getElementById("gameID").value);

switch(id) {
case 1:
gamePrice = 0.99;
break;
case 2:
gamePrice = 0.99;
break;
case 3:
gamePrice = 1.99;
break;
case 4:
gamePrice = 1.99;
break;
case 5:
gamePrice = 3.99;
break;
case 6:
gamePrice = 3.99;
break;
case 7:
gamePrice = 0.99;
break;
case 8:
gamePrice = 0.99;
break;
default:
document.getElementById("totalPriceOutput").innerHTML = "Error. Your final price could not be calculated because you selected an invalid game ID.";
break;
}
}

function finalPriceCalculation() {
var daysInputted = document.getElementById("daysReserved").value;
var finalPrice = daysInputted * gamePrice;

document.getElementById("totalPriceOutput").innerHTML = "Your final price is &pound;" + finalPrice + ".";
}
</script>

</body>

关于javascript - 尝试计算价格时出现 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221531/

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