Confusing return; Spoiler Alert!

Tell us what’s happening:
Just to be clear, I have modified the return line to return tdArray; and it works fine, returning the two dimensional array. My question is : how come return "Resulting Array" + tdArray; returns the values in a one dimensional array? What is the mechanism and what changed there? Thank you in advance.
Your code so far

function chunkArrayInGroups(arr, size) {
        let tdArray = [];
        let indexEnd = size;
        console.log("Initial Array and size: ",arr,size);
        for (let i = 0;i < arr.length;i += size) {
                tdArray.push(arr.slice(i,indexEnd));
                indexEnd+=size;
                console.log("Array in loop: ",tdArray);
        }
        return "Resulting Array: " + tdArray;

}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2),"\n -----------------------------");

  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0.

Challenge: Chunky Monkey

Link to the challenge:

Inside of the loop, you are console.loging the array itself, but in your output you are concatenating the array onto the string "Resulting Array: . This is causing your woes; JS isn’t converting the array to a string in a way that shows the levels of nesting. See what happens with

return "Resulting Array: " + JSON.stringify(tdArray)

Thanks a lot my guy! Just as a future reference… how could I have deduced it from JS documentation? What question should I have asked myself in order to get the answer by myself? :smiley:

I had a hunch that it was a printing problem because I’m always suspicious of JS’s automatic type coercision, so I just Googled “javascript print array” and looked at the first few hits on StackOverflow. A hunch + Google helps frighteningly often :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.