简体   繁体   English

为什么两个数字相乘会出现NaN错误?

[英]Why am I getting a NaN error when multiplying two numbers?

My getPercentage function works perfectly, but my getPayment function doesn't even though it's written exactly the same. 我的getPercentage函数可以完美地工作,但是我的getPayment函数即使写的完全一样也不能。 When I click the "Get Payment" button it returns "NaN" on my HTML. 当我单击“获取付款”按钮时,它在HTML上返回“ NaN”。

 var taxes = { getPercentage: function(total, payment) { var percentage = (payment / total) * 100; return percentage; }, getPayment: function(total, rate) { var payment = (total*rate); return payment; } }; var handlers = { getPercentage: function() { var totalInput = document.getElementById('totalInput'); var paymentInput = document.getElementById('paymentInput'); var answer = taxes.getPercentage(totalInput.value, paymentInput.value); getPercentageAnswer.innerHTML = answer; }, getPayment: function() { var totalInputTwo = document.getElementById('totalInputTwo'); var rateInput = document.getElementById("rateInput"); var answer = taxes.getPayment(totalInputTwo.value, rateInput.value); getPaymentAnswer.innerHTML = answer; } }; 
 <div id="getPayment"> <div id="totalTwo"> Total owed: <input id="totalInputTwo" type="text"> </div> <div id="rateInput"> Your tax rate (as a decimal): <input id="rateInput" type="text"> </div> <button onclick="handlers.getPayment()">Get payment</button> <p id="getPaymentAnswer"></p> </div> 

You have a div and an input with the same id rateInput . 您有一个div和具有相同id rateInputinput So, rateInput.value is trying to get the value from the div and it's returning undefined. 因此, rateInput.value试图从div获取value ,并且返回未定义。 Change the div's id to something else to get the correct input element in rateInput 将div的id更改为其他值,以便在rateInput获得正确的输入元素

 var taxes = { getPayment: function(total, rate) { var payment = (total * rate); return payment; } }; var handlers = { getPayment: function() { var totalInputTwo = document.getElementById('totalInputTwo'); var rateInput = document.getElementById("rateInput"); var answer = taxes.getPayment(+totalInputTwo.value, +rateInput.value); getPaymentAnswer.innerHTML = answer; } }; 
 <div id="getPayment"> <div id="totalTwo"> Total owed: <input id="totalInputTwo" type="text"> </div> <div id="rate"> Your tax rate (as a decimal): <input id="rateInput" type="text"> </div> <button onclick="handlers.getPayment()">Get payment</button> <p id="getPaymentAnswer"></p> </div> 

Note: 注意:

The value of input is always in string format. 输入的值始终为字符串格式。 So, you should always parse them to numbers before doing any mathematical operation. 因此,在执行任何数学运算之前,应始终将它们解析为数字。 When you multiply, the are coerced into numbers. 当您相乘时,会被强制转换为数字。 So, there's no problem here. 因此,这里没有问题。 But, if you were to do addition, the strings will be concatenated instead of their numerical values being added. 但是,如果要进行加法运算,则将字符串连接起来,而不是将其数值相加。

So, you can use the Unary plus operator to convert the strings to numbers: 因此,您可以使用Unary plus运算符将字符串转换为数字:

var answer = taxes.getPayment(+totalInputTwo.value, +rateInput.value);

You should add some validation to your input before doing some calculations. 在进行一些计算之前,您应该在输入中添加一些验证。 Validations like: 验证如下:

  1. Checking if input is number and if not number then what to show to the user in such cases. 检查输入是否为数字,如果不是数字,则在这种情况下向用户显示什么。
  2. If input is empty and then user clicks button to calculate result. 如果输入为空,则用户单击按钮以计算结果。

You should handle these situations first, then your code will work better. 您应该首先处理这些情况,然后您的代码才能更好地工作。

The bug in code is solved by @adiga, but I am providing one of the ways t validate input: @adiga解决了代码中的错误,但是我提供了一种验证输入的方法:

 var taxes = { getPayment: function(total, rate) { var payment = (total * rate); return payment; } }; var isNumber = function(value) { return /^\\d+$/.test(value.trim()) } var handlers = { getPayment: function() { var totalInputTwo = document.getElementById('totalInputTwo'); var rateInput = document.getElementById("rateInput"); // validating code var totalInputTwo_value = totalInputTwo.value.trim() if(isNumber(totalInputTwo.value) && isNumber(rateInput.value)) { var answer = taxes.getPayment(totalInputTwo.value, rateInput.value); getPaymentAnswer.innerHTML = answer; } else { alert("Input is not number of empty. Please check!"); } // console.log(typeof totalInputTwo.value); } }; 
  <div id="getPayment"> <div id="totalTwo"> Total owed: <input id="totalInputTwo" type="text"> </div> <div id="rate"> Your tax rate (as a decimal): <input id="rateInput" type="text"> </div> <button onclick="handlers.getPayment()">Get payment</button> <p id="getPaymentAnswer"></p> </div> 

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

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