gpt4 book ai didi

JavaScript 转换器

转载 作者:行者123 更新时间:2023-12-02 19:40:44 26 4
gpt4 key购买 nike

我在完成这个用于转换咖啡价格的 JavaScript 时遇到问题。我下载了一个调试器,它一直告诉我价格未定义,我不明白。这是脚本。

<html>
<head>
<title>Coffee House</title>
</head>
<body>
<script type="text/javascript">
var drink, ounces, price;

prompt ( "Enter the drink type: espresso, latte, cappuccino, americano" );
prompt ( "Enter the oz: 8, 12, 16" )

var shots = prompt ( "Enter the number of shots" )

if ( drink == "espresso")
price = 1.40;

if (( drink == "latte") || (drink == "cappuccino" )) {
if ( ounce == 8 )
price = 1.95;
else if ( ounce == 12 )
price = 2.35;
else if ( ounce == 16 )
price = 2.75;
}

if ( drink == "americano" )
price = 1.20 + ( ( (ounce -8)/8 ) * .30 );

price = price + ( (shots) * .50 );
price = "price" * 1.055;
price = Math.round( price * 100 )/100;
alert( "Your " + ounce + "oz. " + drink + "with " + shots + "shots of espresso costs: $ " +price );
</script>
</body>
</html>

最佳答案

您的 JavaScript 有几个问题。

  1. 首先,更新 JavaScript 以声明“price”变量。var 饮料,盎司,价格;
  2. 接下来,您需要根据“提示”语句的响应设置变量。

    var 盎司=提示(“输入盎司:8,12,16”)

    var shot=prompt(“请输入镜头数量”)

  3. 您正在对变量“盎司”而不是“盎司”进行字符串比较。这是修正上面1和2后的 undefined variable 。

  4. 您正在对可变价格进行数学计算,尽管您在一个地方将其用作字符串而不是变量值。进行算术运算时,请删除“价格”中的引号。

应用这些更改后,您将看到一些效果:你的 8 盎司。拿铁加 1 杯浓缩咖啡成本:2.58 美元

关于JavaScript 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425087/

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