2016-02-26 62 views
0

我用下面的函數解決了FreeCodeCamp的一個算法挑戰,但是我想知道這是否是一個「好」的方法來解決這個問題,最具體地說,因爲我將計數器設置爲i + = 0並且從頭開始拼接索引。我在這裏創建了一個反模式嗎?有什麼更合乎邏輯的,你能解釋爲什麼嗎?預先感謝您的幫助!將陣列拆分爲二維陣列:是否有更好的解決方案?

function chunk(arr, size) { 
    var newArr = []; 
    for (i=0; i<arr.length; i+=0) { 
     var sliced = arr.slice(i, size); 
     newArr.push(sliced); 
     arr.splice(0, size); 
    } 
    return newArr; 
} 
chunk([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); 
returns--> [ [ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ], [ 8 ] ] 
+1

你可以看看'Array.slice()'。此外,如果這個問題的目標是尋求改進,你應該嘗試CodeReviews,而不是 – Rajesh

+1

我投票結束這個問題作爲題外話,因爲它要求codereview。請訪問codereview.stackexchange.com –

回答

1

有我創建了一個反模式嗎?有什麼更合乎邏輯的,你能解釋爲什麼嗎?

你的代碼包含了一些可改進的零件

function chunk(arr, size) { 
    //newWhatever, myWhatever, ... look for a better naming like `out` or `result` 
    //or in this case `chunks` would describe the content of the variable 
    var newArr = []; 

    //you initialize `i` without the var-keyword, therefore you populate/pollute the global namespace 
    //and instead of calculating i+=0, you can leave this part empty: 
    //for(var i=0; i<arr.length;){ 
    for (i=0; i<arr.length; i+=0) { 
     var sliced = arr.slice(i, size); 
     newArr.push(sliced); 

     //splicing (with P) is often a thing that should be avoided, 
     //- it is destructive (it destroys the input-array) 
     //- it is slow, cause the engine has to allocate new memory 
     // and copy the remaining elements over to this memory, 
     // and garbage-collect the old memory 
     arr.splice(0, size); 
    } 
    return newArr; 
} 

一個更好的解決辦法是:

function chunk(arr, size) { 
    for(var chunks=[], i=0; i<arr.length; i+=size) 
     chunks.push(arr.slice(i, size)); 
    return chunks; 
} 

假設輸入是正確的。

對於完整版,您應該添加一些輸入驗證。該arr已到位,可以切片,並具有長度屬性。
而且這個大小是一個> 0的整數,否則代碼可能會產生奇怪的結果。

function chunk(arr, size) { 
    //checks if arr is not empty and arr.slice is not empty 
    //and casts the length-property to int 
    //if anything "fails" len = 0; 
    var len = (arr && arr.slice && arr.length)|0; 

    //check if size is > 1 and is an integer 
    if(size !== Math.floor(size) || size < 1) 
     throw new Error("invalid chunl-size: "+size); 

    for(var chunks=[], i=0; i<len; i+=size) 
     chunks.push(arr.slice(i, size)); 
    return chunks; 
} 
1

遞歸,也許?

function chunk(arr, size, out) { 

    // if the output array hasn't been passed in 
    // create it 
    out = out || []; 

    // if there are no elements in the input array 
    // return the output array 
    if (!arr.length) return out; 

    // push the "head" of the input array to the 
    // output array 
    out.push(arr.slice(0, size)); 

    // call chunk again with the "tail" of the input array 
    return chunk(arr.slice(size), size, out); 
} 

DEMO

0
function chunk(arr, size) { 
    var subArrayCount = arr.length/size; 
    var res = []; 
    for (i = 0; i < subArrayCount; i++) { 
     var from = size * i; 
     var to = (size * (1 + i)); 
     console.log(to) 
     var sliced = arr.slice(from, to); 
     res.push(sliced); 
    } 
    return res; 
} 
chunk([0, 1, 2, 3, 4, 5, 6, 7, 8], 4); 
returns--> [ [ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ], [ 8 ] ]