Javascript数组。将计算值推送到数组会返回未定义的值

Javascript数组。将计算值推送到数组会返回未定义的值,javascript,arrays,forms,Javascript,Arrays,Forms,我创建了一个表单,需要在其中输入数值,然后计算数值并填充到另一个表单中。然后,需要将计算出的输出添加到数组中,以便在会话的后面进行引用 processOrder函数中的所有数据都成功地推送到各自的数组中,但一旦我尝试推送到任何已计算的数据,而不是简单的输入,undefined就不会返回函数 非常感谢您的帮助 HTML 单位: 公里数: 单位成本: 送货费: 税前总额: JAVASCRIPT var units = new Array(); var kms = new Array(); var

我创建了一个表单,需要在其中输入数值,然后计算数值并填充到另一个表单中。然后,需要将计算出的输出添加到数组中,以便在会话的后面进行引用

processOrder函数中的所有数据都成功地推送到各自的数组中,但一旦我尝试推送到任何已计算的数据,而不是简单的输入,undefined就不会返回函数

非常感谢您的帮助

HTML


单位:
公里数:
单位成本:
送货费:
税前总额:
JAVASCRIPT

var units = new Array();
var kms = new Array();
var timeStamp = new Array();

var unitCost = new Array();
var delivery  = new Array(); 
var preTax  = new Array();

function calculate(){           
var units = document.getElementById("formUnits").value;           
var kms = document.getElementById("formKms").value;     

if ( units >= 1 && units <= 50 ) 
{
unitCost = 11.1 * units 
} 

if ( kms >= 1 && kms <= 20 ) 
{
delivery = 20
} 

var preTax = unitCost + delivery; 
document.getElementById("perUnitCost").value = unitCost.toFixed(2);   
document.getElementById("deliveryCharge").value = delivery;  
document.getElementById("preTaxTotal").value = preTax.toFixed(2);   

processOrder()

} 

function processOrder(){

units.push(document.getElementById('formUnits').value); 
document.getElementById('formUnits').value=''; 
kms.push(document.getElementById('formKms').value); 
document.getElementById('formKms').value=''; 
timeStamp.push(Date()).value;

add_calcs_to_array()
}

function add_calcs_to_array(){

unitCost.push(document.getElementById('perUnitCost').value); 
document.getElementById('perUnitCost').value='';
delivery.push(document.getElementById('deliveryCharge').value); 
document.getElementById('deliveryCharge').value=''; 
preTax.push(document.getElementById('preTaxTotal').value); 
document.getElementById('preTaxTotal').value=''; 

}
var units=new Array();
var kms=新数组();
var timeStamp=新数组();
var unitCost=新数组();
var delivery=新数组();
var preTax=新数组();
函数calculate(){
var units=document.getElementById(“formUnits”).value;
var kms=document.getElementById(“formKms”).value;

如果(units>=1&&units=1&&kms,我看到的问题是,您的变量应该在calculate中初始化为var,以隐藏全局变量。如下所示:

// This is a global variable.
Var unitCost = [];

function calculate(units) {
  // The var unitCost here occult the global unitCost variable.
  // This means that this unitCost is local to the function.
  var unitCost = 0;
  if (units) {
    unitCost = units * 11.1;
  }
}

calculate(2);
console.log(unitCost); // Still []. Without var in calculate, it would be a 22.2.

我遇到的一个错误是,您将
unitCost
定义为数组,但在
calculate
中,您为其赋值,因此不再将其作为数组。因此,我以后是否应该将其声明为数组?它们是两种不同的变量类型。只需为其中一种变量选择不同的名称。请减少发布的代码的大小5x-10x。
// This is a global variable.
Var unitCost = [];

function calculate(units) {
  // The var unitCost here occult the global unitCost variable.
  // This means that this unitCost is local to the function.
  var unitCost = 0;
  if (units) {
    unitCost = units * 11.1;
  }
}

calculate(2);
console.log(unitCost); // Still []. Without var in calculate, it would be a 22.2.