3

I am a little baffled by this, but I have written a function that counts the number of dimensions in an array. This code executed just fine in my work environment, but now that I am in my personal environment, the function (countDims()) no longer returns a value. It appears to execute all the way up to the return statement. Here is the jsfiddle. Thoughts on why this might be?

And a snippet.

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)

2
  • Seems like countDims is recursive, yet you do not return from within it. Commented Dec 29, 2017 at 22:58
  • 1
    This never should have worked. You need to return the recursive call return countDims(arr[0], dim_cnt); Commented Dec 29, 2017 at 22:59

2 Answers 2

3

Did you forgot to return in the first condition of 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)

Sign up to request clarification or add additional context in comments.

3 Comments

Ugh, thanks for the answer. It turns out it worked at work because I wrote it correctly the first time and apparently copied an incomplete version. Sorry for the waste of time.
@MarvinWardJr no problems, glad it worked out :)
Indeed. Demerits for inattention (thought it was some quirk in JavaScript, which is a new language for me). Also, wish I could use Github at work! Thanks again.
2

you need to return result of countDims() call.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.