将数组传递给javascript中的函数

时间:2022-12-05 16:02:01

hey guys I actually asked this question some days ago. I am still here trying to make it work. but am getting undefined how so ?

嘿伙计们,我实际上几天前问了这个问题。我还在这里努力让它发挥作用。但我未定义如何?

function map(f, a) {
  var result = [];
  var i;
  for (i = 0; i != a.length; i++) {
    result[i] = f(a[i]);
  }
  document.write('<br>the result of the computation is: ' +
    result[i]);
}

var myArrays = [2, 3, 4, 5];
var myFunction = function(x) {
  return x * x * x;
}
map(myFunction, myArrays);

I actually thought I had done enough to get this to work.

我实际上认为我已经做了足够的工作来实现这个目标。

What am I missing here?

我在这里想念的是什么?

2 个解决方案

#1


0  

for (INITIALIZATION; CONDITION; AFTERTHOUGHT) {}

for(INITIALIZATION; CONDITION; AFTERTHOUGHT){}

By the time you are logging it, value of i is incremented hence you are accessing an item from array which does not exist, there is no item at mentioned index so value is undefined.

当你记录它时,i的值递增,因此你从数组中访问一个不存在的项,在提到的索引处没有项,所以value是未定义的。

Pass result instead of result[i] or better to use console.log for debugging and pass argument as result. Rest of the code is working fine.

传递结果而不是result [i]或更好地使用console.log进行调试并传递参数作为结果。其余的代码工作正常。

function map(f, a) {
  var result = [];
  var i;
  for (i = 0; i != a.length; i++) {
    result[i] = f(a[i]);
  }
  console.log('<br>the result of the computation is: ' +
    result);
}

var myArrays = [2, 3, 4, 5];
var myFunction = function(x) {
  return x * x * x;
}
map(myFunction, myArrays);

#2


0  

its undefined as you increment i past the array length in the for loop i++, then try to retrieve result[i] in document.write. remove the [i]:-

当你在for循环i ++中增加i超过数组长度时,它是未定义的,然后尝试在document.write中检索result [i]。删除[i]: -

function map(f, a) {
  var result = [];
  var i;
  for (i = 0; i != a.length; i++) {
    result[i] = f(a[i]);
  }
  document.write('<br>the result of the computation is: ' + result);
}

var myArrays = [2, 3, 4, 5];
var myFunction = function(x) {
  return x * x * x;
}
map(myFunction, myArrays);

thanks that really worked fine , but can you really share more light about what happened that led to the undefined ?

谢谢,这真的很好,但你能真正分享更多关于导致未定义的事情的信息吗?

i starts at 0, then every time the for loop is looped. i gets incremented (i++). when the for loop is finished, i is now 1 higher than the greatest index in myArrays. therefore when you try to get result[i] in the document.write, there is no value at that index, resulting in undefined

我从0开始,然后每次循环for循环。我得到增加(i ++)。当for循环结束时,我现在比myArrays中的最大索引高1。因此,当您尝试在document.write中获取result [i]时,该索引处没有值,导致未定义

#1


0  

for (INITIALIZATION; CONDITION; AFTERTHOUGHT) {}

for(INITIALIZATION; CONDITION; AFTERTHOUGHT){}

By the time you are logging it, value of i is incremented hence you are accessing an item from array which does not exist, there is no item at mentioned index so value is undefined.

当你记录它时,i的值递增,因此你从数组中访问一个不存在的项,在提到的索引处没有项,所以value是未定义的。

Pass result instead of result[i] or better to use console.log for debugging and pass argument as result. Rest of the code is working fine.

传递结果而不是result [i]或更好地使用console.log进行调试并传递参数作为结果。其余的代码工作正常。

function map(f, a) {
  var result = [];
  var i;
  for (i = 0; i != a.length; i++) {
    result[i] = f(a[i]);
  }
  console.log('<br>the result of the computation is: ' +
    result);
}

var myArrays = [2, 3, 4, 5];
var myFunction = function(x) {
  return x * x * x;
}
map(myFunction, myArrays);

#2


0  

its undefined as you increment i past the array length in the for loop i++, then try to retrieve result[i] in document.write. remove the [i]:-

当你在for循环i ++中增加i超过数组长度时,它是未定义的,然后尝试在document.write中检索result [i]。删除[i]: -

function map(f, a) {
  var result = [];
  var i;
  for (i = 0; i != a.length; i++) {
    result[i] = f(a[i]);
  }
  document.write('<br>the result of the computation is: ' + result);
}

var myArrays = [2, 3, 4, 5];
var myFunction = function(x) {
  return x * x * x;
}
map(myFunction, myArrays);

thanks that really worked fine , but can you really share more light about what happened that led to the undefined ?

谢谢,这真的很好,但你能真正分享更多关于导致未定义的事情的信息吗?

i starts at 0, then every time the for loop is looped. i gets incremented (i++). when the for loop is finished, i is now 1 higher than the greatest index in myArrays. therefore when you try to get result[i] in the document.write, there is no value at that index, resulting in undefined

我从0开始,然后每次循环for循环。我得到增加(i ++)。当for循环结束时,我现在比myArrays中的最大索引高1。因此,当您尝试在document.write中获取result [i]时,该索引处没有值,导致未定义