简体   繁体   English

为什么我的小费计算器总是得到 NaN 值?

[英]Why i keep getting NaN value in my tip calculator?

I can't find out what is going wrong in my code.我无法找出我的代码出了什么问题。 Thank you.谢谢你。 I have attached the link to code via codepen.我已经通过 codepen 附加了代码的链接。

https://codepen.io/tenzin12/pen/rNmmPbv https://codepen.io/tenzin12/pen/rNmmPbv

`const confirmBtn = document.querySelector(".confirm");

const tipField = document.querySelector(".p1");
const totalField = document.querySelector(".p2");

const tipPercentage = document.querySelector("#tip").children;
const customTip = document.querySelector(".custom").value;

const inputAmt = document.querySelector("#amount").value;
const totalPerson = document.querySelector(".number_of_people").value;

const calcFunction = (bill, percent, diners) => {
  const percentage = percent / 100;
  const tipPerPerson = (bill * percentage) / diners;

  const finalBillPerPerson = bill / diners;
  const finalWithTip = finalBillPerPerson + tipPerPerson;

  tipField.textContent = tipPerPerson;
  totalField.textContent = finalWithTip;
};

for (let i = 0; i < tipPercentage.length; i++) {
  tipPercentage[i].addEventListener("click", () => {
    if (parseInt(totalPerson) > 0) {
      if (tipPercentage[i].value.toUpperCase() === "CUSTOM") {
        calcFunction(parseFloat(inputAmt), parseInt(customTip), parseInt(totalPerson));
      }
    }
    calcFunction(parseFloat(inputAmt), parseInt(tipPercentage[i].value), parseInt(totalPerson));
  });
}
`

When you need to run calculations on element values, you need to collect those values at the time of calculation.当您需要对元素值运行计算时,您需要在计算时收集这些值。 You were collecting them up front - but then when you were calculating the function, it was using those old values.您预先收集它们 - 但是当您计算函数时,它使用的是那些旧值。 I moved those into your function.我将它们移到您的函数中。 Note how I got rid of most of the parseInt and parseFloat functions in favor of the minimal + operator which does the same thing.请注意我是如何摆脱大部分parseIntparseFloat函数的,转而使用执行相同操作的最小+运算符。

Additionally, I simplified the code a little and put in a validation to prevent totals being run on 0 people or 0 amounts.此外,我稍微简化了代码并进行了验证,以防止对 0 人或 0 金额运行总计。 Finally, I changed your for loop into an HTMLCollection forEach loop.最后,我将您的for循环更改for HTMLCollection forEach循环。 I find it is easier to read and maintain我发现它更容易阅读和维护

const confirmBtn = document.querySelector(".confirm");
const tipField = document.querySelector(".p1");
const totalField = document.querySelector(".p2");
const tipPercButtons = document.querySelectorAll("#tip input.percentage");

const calcFunction = (bill, percent, diners) => {
    const percentage = percent / 100;
    const tipPerPerson = (bill * percentage) / diners;

    const finalBillPerPerson = bill / diners;
    const finalWithTip = finalBillPerPerson + tipPerPerson;

    tipField.textContent = tipPerPerson;
    totalField.textContent = finalWithTip;
};

tipPercButtons.forEach((el) =>
    el.addEventListener("click", (e) => {
        const customTip = +document.querySelector(".custom").value;
        const inputAmt = +document.querySelector("#amount").value;
        const totalPerson = +document.querySelector(".number_of_people").value;
        if (isNaN(totalPerson) || isNaN(inputAmt)) {
            alert("Please designate the number of people and the amount of the bill")
            return;
        }
        if (totalPerson === 0) return;
        let val
        if (e.target.value.toUpperCase() === "CUSTOM") val = customTip;
        else val = parseInt(e.target.value);
        calcFunction(inputAmt, val, totalPerson);
    })
);

Updated pen: https://codepen.io/john-tyner/pen/MWmmLMQ?editors=1111更新笔: https : //codepen.io/john-tyner/pen/MWmmLMQ?editors=1111

i analysed your code there is some error in fetching the input value in the code.我分析了您的代码,在获取代码中的输入值时出现了一些错误。 below is the correct code.下面是正确的代码。 Hope this might work make the following little changes in your code:希望这可能会在您的代码中进行以下小改动:

const inputAmt = document.querySelector("#amount");
const totalPerson = document.querySelector(".number_of_people");

and this at the bottom outside the if block这在 if 块外的底部

calcFunction(
  parseFloat(inputAmt.value),
  parseInt(tipPercentage[i].value),
  parseInt(totalPerson.value)
);

overall your calculator is So interesting.总的来说你的计算器很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM