2017-12-29 10 views
3

私はこれに少し戸惑っていますが、配列の次元数を数える関数を書いています。このコードは私の仕事環境で正常に実行されましたが、私の個人的な環境では、関数(countDims())は値を返さなくなりました。これは、return文まですべて実行されているようです。ここにはjsfiddleがあります。なぜこれが起こっているのだろうか?Javascript関数が戻り値に失敗する

スニペット。

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)

+0

'countDims'は再帰的で、まだあなたはその中から戻らないように思えます。 –

+1

これは決してうまくいかなかったはずです。再帰呼び出し 'return countDims(arr [0]、dim_cnt);を返す必要があります。 – Carcigenicate

答えて

3

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)

+1

ああ、答えに感謝します。私はそれを正しく書いたので、それは仕事で働いたことが判明し、不完全なバージョンを明らかにコピーしました。時間の無駄をおかけして申し訳ありません。 –

+0

@MarvinWardJr問題なし、うまくいきました:) –

+0

確かに。不注意のためのデメリット(それは私の新しい言語であるJavaScriptのいくつかの奇妙なものだと思った)。また、職場でGithubを使うことができたらいいなあと思っています。再度、感謝します。 –

2

あなたはcountDims()呼び出しの結果を返す必要があります。

if (Array.isArray(arr)) { 
    dim_cnt++; 
    // Here 
    return countDims(arr[0], dim_cnt); 
} else { 
関連する問題