Chunky Monkey failing validation, but works in console

Chunky Monkey failing validation, but works in console
0

#1

Tell us what’s happening:
Hey everyone,

I have tried multiple solutions to this, using loops and redundant formulas.

I checked them all in console, with debugger, and they all worked for me, yet all answers return as incorrect. Could someone point me in the right direction?

thanks in advance!

your code so far

 //set a global var for answer array
 var ansArr = [];

function chunkArrayInGroups(arr, size) {
    //call newly created redundancy function 
     chunks(arr,size);

//return ansArr
  return ansArr;
}

//create redundancy formula with two parameters 
 function chunks(arr, size) {

 //duplicate the array so I am not changing the original array, not necessary but good practice. 
   var arrCopy = arr.slice();

//test if copied array is longer or the same length as the sizes parameters
    if(arrCopy.length>=size){

// if conditional is true push a splice of array copy to ansArr
    ansArr.push(arrCopy.splice(0,size));

//rerun function with arrCopy passed in to the arr parameter. 
   chunks(arrCopy,size);
  }
  }

chunkArrayInGroups(["a", "b", "c", "d"], 2);

Here is the version of for loops I wrote, I like the redundancy formula a bit more, I just think it would be fast than having a double loop, but the below should still be a viable solution…

//I don’t have this commented out, and I didn’t copy the original array since I added that step later after thinking about it, but if you have any questions I am more than happy to answer them.

var ansArr = [];
function chunkArrayInGroups(arr, size) {
  var temp = [];
 for (i=0; i<=arr.length/size;i++){ 
  for(j=0;j<size;j++){
    temp.push(arr[0]);
     arr.shift();  
   
  }
    ansArr.push(temp);
   temp =[];
 }
  return ansArr;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);

Your browser information:

Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/chunky-monkey

#2

That global declaration of ansArr is causing trouble in both solutions.

I added to console.log statements for the first two tests, so you can see what is getting evaluated by the tests. Run the script below to see what you get.

You need to rewrite your solution so that what ever array you return from chunkArrayInGroups, it is not globally declared or else that array will keep growing with the previous test’s results in the array also.


#3

thanks for the response, I get the issue you are pointing out with the global variable, but should the first validation still pass? when I run it I get

[[a, b], [c, d]]


#4

I assume you meant [[“a”, “b”], [“c”, “d”]] and not [[a, b], [c, d]].

If it is the first results above, then I am not exactly sure why the first test does not pass. It probably has something to do with the way the test works. Maybe someone else can explain the “why”. I will study the code which actually performs to tests, to see if I can figure something out.

In general, when possible, do not use global declarations. Think if there was a huge project with multiple files and many people working on the project. If someone happened to use the same variable name in their portion of the code and you had declared a global variable, things could get wacky very quickly and it would be hard to debug.


#5

There is a way salvage your code if you implemented the following:

  1. Declare the empty array ansArr in your chuckArrayInGroups function.

  2. Create a third parameter in your chunks function and make sure to always pass the ansArr to it (you call this function in two places).

  3. You will need to return asnArr at the end of the chunks function, so any function which called chunks will have the current value of it.

  4. In your chunkArrayInGroups function, you can simply return the result of calling the chunks function.

Hopefully, this helps you not have to completely rewrite your original solution.


#6

Thank you! I will give this a try!


#7

For the 2nd version you had with the for loops, moving the ansArr inside is a good start. The problem with your 2nd version is the arr.shift(); I have move the ansArr inside your chunkArrayInGroups function and added several console.log statements.

function chunkArrayInGroups(arr, size) {
 var ansArr = [];
 var temp = [];
 
 for (i=0; i<=arr.length/size;i++){ 
  console.log('i = ' + i);
  for(j=0;j<size;j++){
    temp.push(arr[0]);
    console.log(arr);
     arr.shift();  
    console.log(arr);
  }
  
  console.log('arr.length/size = ' + arr.length/size + '\n');
  ansArr.push(temp);
  temp =[];
 }
 console.log('\nfinal value of i is ' + i +'\n')
  return ansArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) // should return [[0, 1], [2, 3], [4, 5]]

Below is the result of running the above code. Since you are shrinking arr whenever you shift(), the length of arr is decreasing but variable strong texti is still increasing. At some point your for loop condition of i <= arr.length/size is no longer true. In the test case below, i becomes 2 which is greater than arr.length/size so nothing else happens after that. arr still has the values 4 and 5 in it at the end.

i = 0
[ 0, 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]
[ 2, 3, 4, 5 ]
arr.length/size = 2

i = 1
[ 2, 3, 4, 5 ]
[ 3, 4, 5 ]
[ 3, 4, 5 ]
[ 4, 5 ]
arr.length/size = 1


final value of i is 2

=> [ [ 0, 1 ], [ 2, 3 ] ]