Tell us what’s happening:
My commented out comments I think show that I am quite unsure of the appropriate method to tackle this final basic algorithm challenge. Funny enough, I am getting the appropriate printout in the console…It’s just that It’s not within another array. I’m assuming that I have to create another for loop to make the 2nd dimensional array? This quite a difficult end challenge.
Your code so far
/*Basic Algorithm Scripting: Chunky Monkey
Write a function that splits an array (first argument)
into groups the length of size (second argument) and returns
them as a two-dimensional array.*/
/*take the first group of information in the array and use the split() method
into different groups which will have to be determined by the 'size'
parameter */
function chunkArrayInGroups(arr, size) {
let newArr = []
outter: for(let i = 0; i < arr.length; i ++){
// inner: for(let j = 0; j < arr.length; j++){
// console.log(arr[j]);
let firstArr = (arr.slice(0,size));
newArr.push(firstArr);
let secondArr = (arr.slice(size));
newArr.push(secondArr);
}
return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.
hmmm. I think you may be right that I’m over thinking it.
let newArr = []
outter: for(let i = 0; i < arr.length; i ++){
console.log(arr[i])
inner: for(let j = 0; j < size; j++){
console.log(arr[j]);
newArr.push(arr.slice(size));
}
}
return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));```
I think looping through the array and the size argument would make sense, I just don't understand in a way to slice it appropriately as such. But I will try to think of a more simpler solution.
Sorry just got home from work. I wish I had more time to program!!! I can’t wait to change careers. I’m not quite sure I understand what you’re saying. It sounds like I am running the outer loop through the original array and then the inner loop through the length of the size argument. But that can’t be right as I am getting undefined.
function chunkArrayInGroups(arr, size) {
let newArr = []
outer: for(let i = 0; i < arr.length; i ++){
console.log(arr[i])
inner: for(let j = 0; j < size; j++){
console.log(arr[j][i]);
newArr.push(arr[j][i])
}
}
return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));
I’m sorry that’s insanely confusing. I don’t think I know nested for loops that well enough to grasp what you’re saying. The outer loop isn’t the array length? Then is must be just the array itself:
for(i =0 ; i < arr; i++)
and the inner array isn’t running through the length of the size?
am I incrimenting it by the size?
for(let i = 0; i < arr[i]; size)???
/*Basic Algorithm Scripting: Chunky Monkey
Write a function that splits an array (first argument)
into groups the length of size (second argument) and returns
them as a two-dimensional array.*/
/*take the first group of information in the array and use the split() method
into different groups which will have to be determined by the 'size'
parameter */
function chunkArrayInGroups(arr, size) {
let newArr = []
outer: for(let i = 0; i < size; i++){
console.log(arr[i]);
inner: for(let j = 0; j < arr[i].length; j++){
newArr.push(arr[i][j]);
}
}
return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));
I’m not sure where the the double loops came in, but I suspect this is becoming more complex than you want @CodingDutchman.
How would you do this task by hand? I give you a list of 20 numbers and ask you to copy the list onto a fresh sheet of paper, but put three numbers on each line of your new sheet of paper instead of one. What steps, exactly, do you take to accomplish this task?
OK, so how how can we start making that description sound like a loop? What is the task that is repeated? How do you know when you have done it enough times?
The condition part of the for loop would be the task that is repeated until that condition is met. I don’t understand how to apply that to the parameter array though? I guess that’s where I am getting lost.
the idea is to make
(array.length divided by size) number of arrays
and push them into a container array
and one more array if needed to hold any left overs ,
also pushed into the container array
the arrays are populated with size number
of items if any items are left they populate
one more array which is pushed into the
container
function chunkArrayInGroups ( arr, size ) {
//outer loop
pseudo make an array named tempArray;
pseudo make tempArray equal an empty array;
pseudo make an array named finalArray;
pseudo make finalArray equal an empty array;
pseudo make a variable named length;
pseudo set length to the integer value of arr.length / size;
pseudo make outerLoop run length number of times{
// the inner loop
pseudo make innerLoop run size number of times{
// here we copy out `size` items at a time by
// pushing arr[items] into tempArray
// we will not use arr[index]
// instead we will shift off the items
// https://freecodecamp.github.io/wiki/en/js-array-prototype-shift
pseudo push the shifted values into tempArray;
};
// end of inner loop
//the tempArray will be pushed into finalArray
pseudo push tempArray into finalArray;
// then tempArray is re initalized
pseudo make tempArray equal an empty array;
}//end of outer loop
pseudo chech if any values are left in arr{
pseudo push arr into finalArray
};
return finalArray
}
You said the task is to copy size (3) numbers per line. How do you know to stop copying numbers from the list I gave you?
Edit: I’m off to bed, but I’d recommend you try to look at how you know which size numbers to copy onto each line (or into each new ‘chunk’ array). You’re going to need to loop through your entire list, copying out size items at a time.