FreeCodeCampBeta - Chunky Monkey - Need Help

FreeCodeCampBeta - Chunky Monkey - Need Help
0

#1

Hello, i need help :confused: I’m looking a solution following what i already did with my code. I tried to figured it out but i have a problem with this code. Some thing don’t work with my array.

I don’t know if it’s because i’m getting tired. I should maybe sleep on it and look at it tomorrow with a fresher mind. Maybe not a good idea to seek help :confused:

/*
Link: https://beta.freecodecamp.org/en/challenges/basic-algorithm-scripting/chunky-monkey
*/

function chunkArrayInGroups( arr, size ) {
	let newArr = [];
	var size2 = size;
	let arrL = arr.length;
	let calc = arrL / size;
	let x = 0;
	let z = 0;

	for( let k = size2; k <= arrL; k += size2 ) {
		newArr[z] = arr.slice(x, k);
		z = z + size2;
		x = x + size2;
	}
	
	let newArr0L = newArr[0].length;

	if( newArr0L > arrL ) {
		newArr[newArr0L] = arr.slice(newArr0L, arrL );
	}
	
	//I have empty but i don't know why ...
	
	/* newArr = newArr.filter(function(e) {
		// return String(e).trim();
	 }); */
	
	return newArr;
};


// console.log( chunkArrayInGroups(["a", "b", "c", "d"], 2) ); //[["a", "b"], ["c", "d"]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) ); //[[0, 1, 2], [3, 4, 5]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) ); //[[0, 1], [2, 3], [4, 5]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) ); //[[0, 1, 2, 3], [4, 5]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) ); //[[0, 1, 2], [3, 4, 5], [6]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) ); //[[0, 1, 2, 3], [4, 5, 6, 7], [8]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2) ); //[[0, 1], [2, 3], [4, 5], [6, 7], [8]]

#2

Inspect newArr already right after your first loop and inside it. That should give you enough hints to move forward.


#3

Hello,
yes i was not in my right mind. I don’t know where it come from but:
z = z + size2; become z = z + 1; or z++;…
It’s normal if i had empty stuff, lol.

Will continue to look at it right now


#4

Ok, i’m happy. I manage to finish this exercice :)))

/*
Link: https://beta.freecodecamp.org/en/challenges/basic-algorithm-scripting/chunky-monkey
*/
function isFloat( n ){
	return n === +n && n !== (n|0);
};

function chunkArrayInGroups( arr, size ) {
	let newArr = [];
	var size2 = size;
	let arrL = arr.length;
	let calc = arrL / size2;
	let x = 0;
	let z = 0;
	
	if( isFloat( calc ) ) {
		let arrL2 = arr.length-1;

		for( let k = size2; k <= arrL2; k += size2 ) {
			newArr[z] = arr.slice(x, k);
						
			z++;
			x = x + size2;
		}

		newArr[z] = arr.slice( newArr.length*size2,  arr.length);

	} else {

		for( let k = size2; k <= arrL; k += size2 ) {
			newArr[z] = arr.slice(x, k);
			
			z++;
			x = x + size2;
		}
	}

	return newArr;
};

console.log( chunkArrayInGroups(["a", "b", "c", "d"], 2) ); //[["a", "b"], ["c", "d"]]
console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) ); //[[0, 1, 2], [3, 4, 5]]
console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) ); //[[0, 1], [2, 3], [4, 5]]
console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) ); //[[0, 1, 2, 3], [4, 5]]
console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) ); //[[0, 1, 2], [3, 4, 5], [6]]
console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) ); //[[0, 1, 2, 3], [4, 5, 6, 7], [8]]
console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2) ); //[[0, 1], [2, 3], [4, 5], [6, 7], [8]]

#5

Notice how you repeat the same loop twice, just with a slightly different upper bound. Can you refactor it to reduce the repetition (DRY principle)?


#6

Hello, i know :slight_smile: I will try to but im not sure