【问题标题】:Javascript function fails return value [duplicate]Javascript函数失败返回值[重复]
【发布时间】:2018-06-10 06:55:41
【问题描述】:

我对此有点困惑,但我写了一个函数来计算数组中的维数。这段代码在我的工作环境中执行得很好,但现在我在我的个人环境中,函数 (countDims()) 不再返回值。它似乎一直执行到 return 语句。这是jsfiddle。关于为什么会这样的想法?

还有一个sn-p。

function constantArray(val,...dim){
  // Function returns an nd-array of the given constant value. Note that the ellipsis in
  // the function definition enables a variable number of arguments. Note that at least one
  // dimension value must be given, and all desired dimension extents must be defined as
  // integer lengths.
  arr_out = [];
  // The initial value forms the kernel of the array
  for (i = 0; i < dim[dim.length - 1]; i++) {
    arr_out.push(val);
  }
  // Reducing the dimension list on each pass provides a natural stopping point for recursion
  dim.pop(dim[dim.length - 1]);
  if (dim.length == 0) {
    return arr_out;
  }
  else {
    // Note that the ellipsis in the function call allows us to pass the remaining dimensions
    // as a list. In this context, the ellipsis is the "spread" operator.
    return constantArray(arr_out, ...dim);
  }
}

function countDims(arr, dim_cnt){
  // Function returns the number of dimensions in an array. Note that we keep the dimension
  // count in the function arguments to ease updating during recursive calls.
    if (dim_cnt == undefined) {dim_cnt = 0};
    if (Array.isArray(arr)) {
    dim_cnt++;
    countDims(arr[0], dim_cnt);
    }
    else {
      console.log("The dimension count of this array is "+dim_cnt);
      console.log("I am in the return space!")
      return dim_cnt;
    }
}

x = constantArray(0, 4, 5)
console.log(x)
x_dims = countDims(x)
console.log(x_dims)

【问题讨论】:

  • 似乎countDims 是递归的,但你并没有从其中返回。
  • 这不应该奏效。你需要返回递归调用return countDims(arr[0], dim_cnt);

标签: javascript


【解决方案1】:

你是不是忘了countDims的第一个条件返回?

if (Array.isArray(arr)) {
    dim_cnt++;
    return countDims(arr[0], dim_cnt);

function constantArray(val, ...dim) {
  // Function returns an nd-array of the given constant value. Note that the ellipsis in
  // the function definition enables a variable number of arguments. Note that at least one
  // dimension value must be given, and all desired dimension extents must be defined as
  // integer lengths.
  var arr_out = [];
  // The initial value forms the kernel of the array
  for (let i = 0; i < dim[dim.length - 1]; i++) {
    arr_out.push(val);
  }
  // Reducing the dimension list on each pass provides a natural stopping point for recursion
  dim.pop(dim[dim.length - 1]);
  if (dim.length == 0) {
    return arr_out;
  } else {
    // Note that the ellipsis in the function call allows us to pass the remaining dimensions
    // as a list. In this context, the ellipsis is the "spread" operator.
    return constantArray(arr_out, ...dim);
  }
}

function countDims(arr, dim_cnt = 0) {
  // Function returns the number of dimensions in an array. Note that we keep the dimension
  // count in the function arguments to ease updating during recursive calls.
  //if (dim_cnt == undefined) {dim_cnt = 0};
  if (Array.isArray(arr)) {
    dim_cnt++;
    return countDims(arr[0], dim_cnt);
  } else {
    console.log("The dimension count of this array is " + dim_cnt);
    console.log("I am in the return space!")
    debugger
    return dim_cnt;
  }
}

var x = constantArray(0, 4, 5)
console.log(x)
var x_dims = countDims(x)
console.log(x_dims)

【讨论】:

  • 呃,谢谢你的回答。事实证明它起作用了,因为我第一次正确地写了它并且显然复制了一个不完整的版本。很抱歉浪费了时间。
  • @MarvinWardJr 没问题,很高兴它成功了 :)
  • 确实如此。注意力不集中的缺点(认为这是 JavaScript 中的一些怪癖,这对我来说是一门新语言)。另外,希望我可以在工作中使用 Github!再次感谢。
【解决方案2】:

您需要返回 countDims() 调用的结果。

if (Array.isArray(arr)) {
    dim_cnt++;
    // Here
    return countDims(arr[0], dim_cnt);
} else {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2015-08-01
    • 2018-08-21
    • 1970-01-01
    • 2013-10-23
    • 2012-09-27
    相关资源
    最近更新 更多