JavaScript - Algorithm exercise - Dividing an array in small partsHow do I check if an array includes an object in JavaScript?Deleting array elements in JavaScript - delete vs spliceHow to insert an item into an array at a specific index (JavaScript)?How do you check if a variable is an array in JavaScript?Sorting an array of JavaScript objects by propertyHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?How can I add new array elements at the beginning of an array in Javascript?For-each over an array in JavaScript?

Does an ice chest packed full of frozen food need ice?

Should an arbiter claim draw at a K+R vs K+R endgame?

At what point in time did Dumbledore ask Snape for this favor?

Was Jesus good at singing?

How to officially communicate to a non-responsive colleague?

Last survivors from different time periods living together

Soft question: Examples where lack of mathematical rigour cause security breaches?

Cross-dimension teleportation using command block or datapack?

Arriving at the same result with the opposite hypotheses

Was there a priest on the Titanic who stayed on the ship giving confession to as many as he could?

What can plausibly explain many of my very long and low-tech bridges?

What is the actual quality of machine translations?

Preventing Employees from either switching to Competitors or Opening Their Own Business

Using a found spellbook as a Sorcerer-Wizard multiclass

Implement Homestuck's Catenative Doomsday Dice Cascader

If you had a giant cutting disc 60 miles diameter and rotated it 1000 rps, would the edge be traveling faster than light?

How Can I Tell The Difference Between Unmarked Sugar and Stevia?

Can a user sell my software (MIT license) without modification?

Was the output of the C64 SID chip 8 bit sound?

Find duplicated column value in CSV

What makes Ada the language of choice for the ISS's safety-critical systems?

Was the Tamarian language in "Darmok" inspired by Jack Vance's "The Asutra"?

How does an ordinary object become radioactive?

How to chain Python function calls so the behaviour is as follows



JavaScript - Algorithm exercise - Dividing an array in small parts


How do I check if an array includes an object in JavaScript?Deleting array elements in JavaScript - delete vs spliceHow to insert an item into an array at a specific index (JavaScript)?How do you check if a variable is an array in JavaScript?Sorting an array of JavaScript objects by propertyHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?How can I add new array elements at the beginning of an array in Javascript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I have this algorithm exercise that splits an array into smaller parts. This is working right, but I did not understand the operation. Could someone explain this to me in more detail. Please.





const letters = ['a', 'b', 'c', 'd', 'e']

function chunk(array, size)
const chunked = [];

for (let element of array)
const last = chunked[chunked.length - 1]

if (!last

return chunked;



console.log(chunk(letters, 2))


It returns:



['a','b']
['c'.'d']
['e']


This parameter that in this case is the number 2, which is passed in the function, is the number of items that each array contains.



console.log(chunk(letters, 2))


I didn't understand the fact that it generated 2 arrays 'chunked' and 'last' and in the end only return 'chunked'. But I did not understand the link between the 2 arrays: 'chunked' and 'last'










share|improve this question



















  • 2





    what exactly you don't understand ?

    – Ali
    Mar 24 at 16:22











  • The fact that it generated 2 arrays 'chunk' and 'last' and in the end only return 'chunked'. But I did not understand the 'last' array link

    – claudiobitar
    Mar 24 at 16:31

















-1















I have this algorithm exercise that splits an array into smaller parts. This is working right, but I did not understand the operation. Could someone explain this to me in more detail. Please.





const letters = ['a', 'b', 'c', 'd', 'e']

function chunk(array, size)
const chunked = [];

for (let element of array)
const last = chunked[chunked.length - 1]

if (!last

return chunked;



console.log(chunk(letters, 2))


It returns:



['a','b']
['c'.'d']
['e']


This parameter that in this case is the number 2, which is passed in the function, is the number of items that each array contains.



console.log(chunk(letters, 2))


I didn't understand the fact that it generated 2 arrays 'chunked' and 'last' and in the end only return 'chunked'. But I did not understand the link between the 2 arrays: 'chunked' and 'last'










share|improve this question



















  • 2





    what exactly you don't understand ?

    – Ali
    Mar 24 at 16:22











  • The fact that it generated 2 arrays 'chunk' and 'last' and in the end only return 'chunked'. But I did not understand the 'last' array link

    – claudiobitar
    Mar 24 at 16:31













-1












-1








-1








I have this algorithm exercise that splits an array into smaller parts. This is working right, but I did not understand the operation. Could someone explain this to me in more detail. Please.





const letters = ['a', 'b', 'c', 'd', 'e']

function chunk(array, size)
const chunked = [];

for (let element of array)
const last = chunked[chunked.length - 1]

if (!last

return chunked;



console.log(chunk(letters, 2))


It returns:



['a','b']
['c'.'d']
['e']


This parameter that in this case is the number 2, which is passed in the function, is the number of items that each array contains.



console.log(chunk(letters, 2))


I didn't understand the fact that it generated 2 arrays 'chunked' and 'last' and in the end only return 'chunked'. But I did not understand the link between the 2 arrays: 'chunked' and 'last'










share|improve this question
















I have this algorithm exercise that splits an array into smaller parts. This is working right, but I did not understand the operation. Could someone explain this to me in more detail. Please.





const letters = ['a', 'b', 'c', 'd', 'e']

function chunk(array, size)
const chunked = [];

for (let element of array)
const last = chunked[chunked.length - 1]

if (!last

return chunked;



console.log(chunk(letters, 2))


It returns:



['a','b']
['c'.'d']
['e']


This parameter that in this case is the number 2, which is passed in the function, is the number of items that each array contains.



console.log(chunk(letters, 2))


I didn't understand the fact that it generated 2 arrays 'chunked' and 'last' and in the end only return 'chunked'. But I did not understand the link between the 2 arrays: 'chunked' and 'last'







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 16:36







claudiobitar

















asked Mar 24 at 16:19









claudiobitarclaudiobitar

401113




401113







  • 2





    what exactly you don't understand ?

    – Ali
    Mar 24 at 16:22











  • The fact that it generated 2 arrays 'chunk' and 'last' and in the end only return 'chunked'. But I did not understand the 'last' array link

    – claudiobitar
    Mar 24 at 16:31












  • 2





    what exactly you don't understand ?

    – Ali
    Mar 24 at 16:22











  • The fact that it generated 2 arrays 'chunk' and 'last' and in the end only return 'chunked'. But I did not understand the 'last' array link

    – claudiobitar
    Mar 24 at 16:31







2




2





what exactly you don't understand ?

– Ali
Mar 24 at 16:22





what exactly you don't understand ?

– Ali
Mar 24 at 16:22













The fact that it generated 2 arrays 'chunk' and 'last' and in the end only return 'chunked'. But I did not understand the 'last' array link

– claudiobitar
Mar 24 at 16:31





The fact that it generated 2 arrays 'chunk' and 'last' and in the end only return 'chunked'. But I did not understand the 'last' array link

– claudiobitar
Mar 24 at 16:31












5 Answers
5






active

oldest

votes


















0














You are creating an array of arrays. The outer array is called chunked. The last array is the last inner array up to this point. chunked looks like:



[[/*some values*/], [/*some values*/], [/* this is last */]]


That's what this line is doing:



const last = chunked[chunked.length - 1] // get the final child array in chunked


You then decide whether to push the current value into this subarray or add a new subarray. This choice is determined by whether the length of last is less than the chunk size — in other words whether it is full. That's what this test is doing:



if (!last || last.length === size) 


The !last part is used to check if there is a last array because on the first iteration it won't exist yet — the outer array is empty.



For what it's worth, it might be easier to understand it you just use a simple loop that increments by chunk size:






const letters = ['a', 'b', 'c', 'd', 'e']

function chunk(array, size)
const chunked = [];

for (let i = 0; i < array.length; i+=size)
chunked.push(array.slice(i, i+size))

return chunked



console.log(chunk(letters, 2))








share|improve this answer






























    0














    Generally there are three things:




    1. last contains the last chunk, freshly taken for each iteration, which grows until it reaches the specified size

    2. when last reaches the specified size, chunked.push([element]) pushes a new array into chunked ([element] without an array in front of it, is a short syntax for creating a new array with a single element - and for the next iteration, that is going to be the last chunk. Try writing console.log(1), console.log([1]) and perhaps console.log([1,2]) into a JS console to see their difference.

    3. if the last chunk has not reached the specified size, the element is just appended to it

    In particular there is one "trick", for the first iteration: when chunked is an empty array at the beginning, last = chunked[chunked.length - 1] translates to chunked[-1]. Unlike many other languages, JavaScript does not die or throw an exception here, it just says the given element is undefined, which is a falsy value and triggers the !last condition, thus the push([element]) thing will run. This is how the first chunk is created.






    share|improve this answer






























      0














      this will get the last element of chunked array , which is either undefined or the last chunk array



      const last = chunked[chunked.length - 1]


      then you check if last is undefined or is an array with size of size then you add new array to chunked array with the current element



      if (!last || last.length === size) 
      chunked.push([element])



      else , which mean that last is an array with size less than size , you push current element to last



      else 
      last.push(element)






      share|improve this answer






























        0














        1. a function chunk which take 2 arguments, an array and length of chunks

        2. a chunked array is defined

        3. using for loop iterating over the array

        4. if the last array is not the last array inside chunked array or last element of chunked array is equal to size argument, then push new element from array array inside another array so that it form a chunked array

        5. else push the new element from array array to last array of chunked array

        6. return chunked array





        share|improve this answer






























          0














          More or less: initialize an empty array chunked.



          Inside a loop: look into chunked's last element. Is it non-existent, or is it already exactly the required length? If yes, take the next letter, make a 1-element array out of it, and append it to chunked. If no, then append the letter to chunked's last element instead.



          Run the code in a debugger, set watch expressions for chunked, last and element and watch what's happening to them.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325872%2fjavascript-algorithm-exercise-dividing-an-array-in-small-parts%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You are creating an array of arrays. The outer array is called chunked. The last array is the last inner array up to this point. chunked looks like:



            [[/*some values*/], [/*some values*/], [/* this is last */]]


            That's what this line is doing:



            const last = chunked[chunked.length - 1] // get the final child array in chunked


            You then decide whether to push the current value into this subarray or add a new subarray. This choice is determined by whether the length of last is less than the chunk size — in other words whether it is full. That's what this test is doing:



            if (!last || last.length === size) 


            The !last part is used to check if there is a last array because on the first iteration it won't exist yet — the outer array is empty.



            For what it's worth, it might be easier to understand it you just use a simple loop that increments by chunk size:






            const letters = ['a', 'b', 'c', 'd', 'e']

            function chunk(array, size)
            const chunked = [];

            for (let i = 0; i < array.length; i+=size)
            chunked.push(array.slice(i, i+size))

            return chunked



            console.log(chunk(letters, 2))








            share|improve this answer



























              0














              You are creating an array of arrays. The outer array is called chunked. The last array is the last inner array up to this point. chunked looks like:



              [[/*some values*/], [/*some values*/], [/* this is last */]]


              That's what this line is doing:



              const last = chunked[chunked.length - 1] // get the final child array in chunked


              You then decide whether to push the current value into this subarray or add a new subarray. This choice is determined by whether the length of last is less than the chunk size — in other words whether it is full. That's what this test is doing:



              if (!last || last.length === size) 


              The !last part is used to check if there is a last array because on the first iteration it won't exist yet — the outer array is empty.



              For what it's worth, it might be easier to understand it you just use a simple loop that increments by chunk size:






              const letters = ['a', 'b', 'c', 'd', 'e']

              function chunk(array, size)
              const chunked = [];

              for (let i = 0; i < array.length; i+=size)
              chunked.push(array.slice(i, i+size))

              return chunked



              console.log(chunk(letters, 2))








              share|improve this answer

























                0












                0








                0







                You are creating an array of arrays. The outer array is called chunked. The last array is the last inner array up to this point. chunked looks like:



                [[/*some values*/], [/*some values*/], [/* this is last */]]


                That's what this line is doing:



                const last = chunked[chunked.length - 1] // get the final child array in chunked


                You then decide whether to push the current value into this subarray or add a new subarray. This choice is determined by whether the length of last is less than the chunk size — in other words whether it is full. That's what this test is doing:



                if (!last || last.length === size) 


                The !last part is used to check if there is a last array because on the first iteration it won't exist yet — the outer array is empty.



                For what it's worth, it might be easier to understand it you just use a simple loop that increments by chunk size:






                const letters = ['a', 'b', 'c', 'd', 'e']

                function chunk(array, size)
                const chunked = [];

                for (let i = 0; i < array.length; i+=size)
                chunked.push(array.slice(i, i+size))

                return chunked



                console.log(chunk(letters, 2))








                share|improve this answer













                You are creating an array of arrays. The outer array is called chunked. The last array is the last inner array up to this point. chunked looks like:



                [[/*some values*/], [/*some values*/], [/* this is last */]]


                That's what this line is doing:



                const last = chunked[chunked.length - 1] // get the final child array in chunked


                You then decide whether to push the current value into this subarray or add a new subarray. This choice is determined by whether the length of last is less than the chunk size — in other words whether it is full. That's what this test is doing:



                if (!last || last.length === size) 


                The !last part is used to check if there is a last array because on the first iteration it won't exist yet — the outer array is empty.



                For what it's worth, it might be easier to understand it you just use a simple loop that increments by chunk size:






                const letters = ['a', 'b', 'c', 'd', 'e']

                function chunk(array, size)
                const chunked = [];

                for (let i = 0; i < array.length; i+=size)
                chunked.push(array.slice(i, i+size))

                return chunked



                console.log(chunk(letters, 2))








                const letters = ['a', 'b', 'c', 'd', 'e']

                function chunk(array, size)
                const chunked = [];

                for (let i = 0; i < array.length; i+=size)
                chunked.push(array.slice(i, i+size))

                return chunked



                console.log(chunk(letters, 2))





                const letters = ['a', 'b', 'c', 'd', 'e']

                function chunk(array, size)
                const chunked = [];

                for (let i = 0; i < array.length; i+=size)
                chunked.push(array.slice(i, i+size))

                return chunked



                console.log(chunk(letters, 2))






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 16:37









                Mark MeyerMark Meyer

                45.7k33971




                45.7k33971























                    0














                    Generally there are three things:




                    1. last contains the last chunk, freshly taken for each iteration, which grows until it reaches the specified size

                    2. when last reaches the specified size, chunked.push([element]) pushes a new array into chunked ([element] without an array in front of it, is a short syntax for creating a new array with a single element - and for the next iteration, that is going to be the last chunk. Try writing console.log(1), console.log([1]) and perhaps console.log([1,2]) into a JS console to see their difference.

                    3. if the last chunk has not reached the specified size, the element is just appended to it

                    In particular there is one "trick", for the first iteration: when chunked is an empty array at the beginning, last = chunked[chunked.length - 1] translates to chunked[-1]. Unlike many other languages, JavaScript does not die or throw an exception here, it just says the given element is undefined, which is a falsy value and triggers the !last condition, thus the push([element]) thing will run. This is how the first chunk is created.






                    share|improve this answer



























                      0














                      Generally there are three things:




                      1. last contains the last chunk, freshly taken for each iteration, which grows until it reaches the specified size

                      2. when last reaches the specified size, chunked.push([element]) pushes a new array into chunked ([element] without an array in front of it, is a short syntax for creating a new array with a single element - and for the next iteration, that is going to be the last chunk. Try writing console.log(1), console.log([1]) and perhaps console.log([1,2]) into a JS console to see their difference.

                      3. if the last chunk has not reached the specified size, the element is just appended to it

                      In particular there is one "trick", for the first iteration: when chunked is an empty array at the beginning, last = chunked[chunked.length - 1] translates to chunked[-1]. Unlike many other languages, JavaScript does not die or throw an exception here, it just says the given element is undefined, which is a falsy value and triggers the !last condition, thus the push([element]) thing will run. This is how the first chunk is created.






                      share|improve this answer

























                        0












                        0








                        0







                        Generally there are three things:




                        1. last contains the last chunk, freshly taken for each iteration, which grows until it reaches the specified size

                        2. when last reaches the specified size, chunked.push([element]) pushes a new array into chunked ([element] without an array in front of it, is a short syntax for creating a new array with a single element - and for the next iteration, that is going to be the last chunk. Try writing console.log(1), console.log([1]) and perhaps console.log([1,2]) into a JS console to see their difference.

                        3. if the last chunk has not reached the specified size, the element is just appended to it

                        In particular there is one "trick", for the first iteration: when chunked is an empty array at the beginning, last = chunked[chunked.length - 1] translates to chunked[-1]. Unlike many other languages, JavaScript does not die or throw an exception here, it just says the given element is undefined, which is a falsy value and triggers the !last condition, thus the push([element]) thing will run. This is how the first chunk is created.






                        share|improve this answer













                        Generally there are three things:




                        1. last contains the last chunk, freshly taken for each iteration, which grows until it reaches the specified size

                        2. when last reaches the specified size, chunked.push([element]) pushes a new array into chunked ([element] without an array in front of it, is a short syntax for creating a new array with a single element - and for the next iteration, that is going to be the last chunk. Try writing console.log(1), console.log([1]) and perhaps console.log([1,2]) into a JS console to see their difference.

                        3. if the last chunk has not reached the specified size, the element is just appended to it

                        In particular there is one "trick", for the first iteration: when chunked is an empty array at the beginning, last = chunked[chunked.length - 1] translates to chunked[-1]. Unlike many other languages, JavaScript does not die or throw an exception here, it just says the given element is undefined, which is a falsy value and triggers the !last condition, thus the push([element]) thing will run. This is how the first chunk is created.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 24 at 16:38









                        tevemadartevemadar

                        5,2032928




                        5,2032928





















                            0














                            this will get the last element of chunked array , which is either undefined or the last chunk array



                            const last = chunked[chunked.length - 1]


                            then you check if last is undefined or is an array with size of size then you add new array to chunked array with the current element



                            if (!last || last.length === size) 
                            chunked.push([element])



                            else , which mean that last is an array with size less than size , you push current element to last



                            else 
                            last.push(element)






                            share|improve this answer



























                              0














                              this will get the last element of chunked array , which is either undefined or the last chunk array



                              const last = chunked[chunked.length - 1]


                              then you check if last is undefined or is an array with size of size then you add new array to chunked array with the current element



                              if (!last || last.length === size) 
                              chunked.push([element])



                              else , which mean that last is an array with size less than size , you push current element to last



                              else 
                              last.push(element)






                              share|improve this answer

























                                0












                                0








                                0







                                this will get the last element of chunked array , which is either undefined or the last chunk array



                                const last = chunked[chunked.length - 1]


                                then you check if last is undefined or is an array with size of size then you add new array to chunked array with the current element



                                if (!last || last.length === size) 
                                chunked.push([element])



                                else , which mean that last is an array with size less than size , you push current element to last



                                else 
                                last.push(element)






                                share|improve this answer













                                this will get the last element of chunked array , which is either undefined or the last chunk array



                                const last = chunked[chunked.length - 1]


                                then you check if last is undefined or is an array with size of size then you add new array to chunked array with the current element



                                if (!last || last.length === size) 
                                chunked.push([element])



                                else , which mean that last is an array with size less than size , you push current element to last



                                else 
                                last.push(element)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 24 at 16:39









                                AliAli

                                6,82731735




                                6,82731735





















                                    0














                                    1. a function chunk which take 2 arguments, an array and length of chunks

                                    2. a chunked array is defined

                                    3. using for loop iterating over the array

                                    4. if the last array is not the last array inside chunked array or last element of chunked array is equal to size argument, then push new element from array array inside another array so that it form a chunked array

                                    5. else push the new element from array array to last array of chunked array

                                    6. return chunked array





                                    share|improve this answer



























                                      0














                                      1. a function chunk which take 2 arguments, an array and length of chunks

                                      2. a chunked array is defined

                                      3. using for loop iterating over the array

                                      4. if the last array is not the last array inside chunked array or last element of chunked array is equal to size argument, then push new element from array array inside another array so that it form a chunked array

                                      5. else push the new element from array array to last array of chunked array

                                      6. return chunked array





                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        1. a function chunk which take 2 arguments, an array and length of chunks

                                        2. a chunked array is defined

                                        3. using for loop iterating over the array

                                        4. if the last array is not the last array inside chunked array or last element of chunked array is equal to size argument, then push new element from array array inside another array so that it form a chunked array

                                        5. else push the new element from array array to last array of chunked array

                                        6. return chunked array





                                        share|improve this answer













                                        1. a function chunk which take 2 arguments, an array and length of chunks

                                        2. a chunked array is defined

                                        3. using for loop iterating over the array

                                        4. if the last array is not the last array inside chunked array or last element of chunked array is equal to size argument, then push new element from array array inside another array so that it form a chunked array

                                        5. else push the new element from array array to last array of chunked array

                                        6. return chunked array






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Mar 24 at 16:44









                                        Sushant MagooSushant Magoo

                                        194112




                                        194112





















                                            0














                                            More or less: initialize an empty array chunked.



                                            Inside a loop: look into chunked's last element. Is it non-existent, or is it already exactly the required length? If yes, take the next letter, make a 1-element array out of it, and append it to chunked. If no, then append the letter to chunked's last element instead.



                                            Run the code in a debugger, set watch expressions for chunked, last and element and watch what's happening to them.






                                            share|improve this answer



























                                              0














                                              More or less: initialize an empty array chunked.



                                              Inside a loop: look into chunked's last element. Is it non-existent, or is it already exactly the required length? If yes, take the next letter, make a 1-element array out of it, and append it to chunked. If no, then append the letter to chunked's last element instead.



                                              Run the code in a debugger, set watch expressions for chunked, last and element and watch what's happening to them.






                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                More or less: initialize an empty array chunked.



                                                Inside a loop: look into chunked's last element. Is it non-existent, or is it already exactly the required length? If yes, take the next letter, make a 1-element array out of it, and append it to chunked. If no, then append the letter to chunked's last element instead.



                                                Run the code in a debugger, set watch expressions for chunked, last and element and watch what's happening to them.






                                                share|improve this answer













                                                More or less: initialize an empty array chunked.



                                                Inside a loop: look into chunked's last element. Is it non-existent, or is it already exactly the required length? If yes, take the next letter, make a 1-element array out of it, and append it to chunked. If no, then append the letter to chunked's last element instead.



                                                Run the code in a debugger, set watch expressions for chunked, last and element and watch what's happening to them.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 24 at 16:44









                                                mbojkombojko

                                                3,1821415




                                                3,1821415



























                                                    draft saved

                                                    draft discarded
















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325872%2fjavascript-algorithm-exercise-dividing-an-array-in-small-parts%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현