【发布时间】:2023-02-20 05:23:40
【问题描述】:
我的脚本的最后两个 console.logs 有一些问题。我应该为两者输出数字,但我得到的是 NAN
alert("Let's make a shopping list!");
let first = prompt("What is the first item?");
let firstCost = Number(prompt("What is the cost of " + first + "?"));
let firstAmount = Number(prompt("How many of " + first + " would you like?"));
let second = prompt("What is the second item?");
let secondCost = Number(prompt("What is the cost of " + second + "?"));
let secondAmount = Number(prompt("How many of " + second + " would you like?"));
let tax = parseInt(prompt("What is the sales tax for your state?"));
let firstTotal = parseFloat(firstCost * firstAmount);
let secondTotal = parseFloat(firstCost * firstAmount);
let subTotal = parseFloat(firstTotal + secondTotal);
let taxTotal = parseFloat(subTotal * tax);
let grandTotal = parseFloat(subTotal + taxTotal);
console.log(first + " " + firstCost + " " + firstAmount + " " +
firstTotal);
console.log(second + " " + secondCost + " " + secondAmount + " " +
secondTotal);
console.log("tax: " + taxTotal);
console.log("TOTAL: " + grandTotal);
我将所有 Number() 更改为 parseFloat() 但我没有得到我正在寻找的结果。
【问题讨论】:
-
提示:Template literals 存在,可以帮助清理这段代码。
-
提示:如果你有数字,你可以在不解析的情况下对它们进行数学运算。他们是已经数字。放下
parseFloat,走开! -
@tadman,我的教授希望我们在这个非常漫长的开始过程中做到这一点。我不允许使用他没有教给我们的任何东西。
标签: javascript