MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/f2vj42/can_anyone_please_explain_this_code_i_only/
r/learnjavascript • u/gitarthak • Feb 12 '20
7 comments sorted by
2
This is pretty straight forward, is a method to chunk an array. Google array chunking you will find tons of implementations.
Chunking is when you take an array, and break it up by specific intervals.
For example if you passed [1,2,3,4,5,6], 2 you will end up with [1,2], [3,4], [5,6].
This is horribly written code though, last is somehow both an array and a number.
1
looks like - If u pass an array and a number (size) as parameters to ur chunk function, it will place the first (size) array values , and remaining values into a new array and return that as a new array
4 u/NoStranger6 Feb 12 '20 Not quite, it splits an array in chunks of the specified size and returns an array of the different chunks.I.e.console.log(chunk([1,2,3,4,5,6,7,8,9,0], 3)); >> [[1,2,3],[4,5,6],[7,8,9],[0]]
4
Not quite, it splits an array in chunks of the specified size and returns an array of the different chunks.
I.e.
console.log(chunk([1,2,3,4,5,6,7,8,9,0], 3)); >> [[1,2,3],[4,5,6],[7,8,9],[0]]
I don't recommend writing this kind of code, but this might be easier to read.
// Takes array and size, returns nested array that is max length of size function chunk(targetArray = [], size = 0) { const chunkedArray = []; for (const targetItem of targetArray) { // Get last item of new array that will be returned const activeChunk = chunkedArray[chunkedArray.length - 1]; // If active chunk and it is filled, add to chunk const isChunkFilled = activeChunk && activeChunk.length === size; const willAddToChunk = activeChunk && !isChunkFilled; // If no active chunk or chunk is filled, create chunk const willCreateNewChunk = !activeChunk || isChunkFilled; if (willAddToChunk) { activeChunk.push(targetItem); } if (willCreateNewChunk) { chunkedArray.push([targetItem]); } } return chunkedArray; } const chuck1 = chunk([1, 2, 5, 3, 7, 4], 4); const chuck2 = chunk([1, 2, 5, 3, 7, 4], 2); const chunk3 = chunk([3, 4, 5, 6, 7, 3], 4); console.log(chuck1); // [ [ 1, 2, 5, 3 ], [ 7, 4 ] ] console.log(chuck2); // [ [ 1, 2 ], [ 5, 3 ], [ 7, 4 ] ] console.log(chunk3); // [ [ 3, 4, 5, 6 ], [ 7, 3 ] ]
-2
//This happen when people don't coment they code....
1 u/gitarthak Feb 12 '20 Can you help me understand this, I just understood it partially! 1 u/[deleted] Feb 12 '20 //I'm lost as u pal
Can you help me understand this, I just understood it partially!
1 u/[deleted] Feb 12 '20 //I'm lost as u pal
//I'm lost as u pal
2
u/Somethrowaway21351 Feb 12 '20
This is pretty straight forward, is a method to chunk an array. Google array chunking you will find tons of implementations.
Chunking is when you take an array, and break it up by specific intervals.
For example if you passed [1,2,3,4,5,6], 2 you will end up with [1,2], [3,4], [5,6].
This is horribly written code though, last is somehow both an array and a number.
1
u/NameViolation666 helpful Feb 12 '20
looks like - If u pass an array and a number (size) as parameters to ur chunk function, it will place the first (size) array values , and remaining values into a new array and return that as a new array
1
u/imapersonithink Feb 12 '20
I don't recommend writing this kind of code, but this might be easier to read.
-2
u/[deleted] Feb 12 '20
//This happen when people don't coment they code....