Basic Algorithm Scripting - Chunky Monkey

Tell us what’s happening:
Describe your issue in detail here.
My code won’t increment my multiplier variable and I’m having trouble visualizing it as I don’t have a debugger. Show me the issue please and also if there’s a resource to help me debug JavaScript visually, let me know!

  **Your code so far**
function chunkArrayInGroups(arr, size) {

let blankArray = [];
let blankArray2 = [];
let multiplier = 1;

for (let i = 0; i < arr.length; i++) {  
  console.log(blankArray.push(arr[i]));  
}

let slice1 = blankArray.slice(0,size); // always slices the correct first element
let slice2 = blankArray.slice(size, size + slice1.length); // always slices the correct 2nd element.
let slice3 = blankArray.slice( 2 * size,  2 * size + slice1.length); //always slice 3rd element.
let slice4 = blankArray.slice( 3 * size,  3 * size + slice1.length); //always slice 4th element. So on and so forth adding 1 to the multiplier.
//console.log(slice);

console.log(slice1);
console.log(slice2);
console.log(slice3);
console.log(slice4);
//blankArray.push(slice2); //adds to the back
//blankArray.unshift(slice2); //adds to the front



blankArray2.push(slice1);
let slice = blankArray.slice(multiplier * size, multiplier * size + slice1.length); // always slices the correct 2nd element and so on as multiplier increments.
for (let i = 0; i < arr.length; i++) {

  if (slice !== []) {  
  blankArray2.push(slice);
  multiplier++;
  
  }
}

return blankArray2;
}

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

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Chunky Monkey

Link to the challenge:

you can add more description to your console.log statements

you are logging bun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
[ 4, 5 ]
[ 6, 7 ]
[
  [ 0, 1 ], [ 2, 3 ],
  [ 2, 3 ], [ 2, 3 ],
  [ 2, 3 ], [ 2, 3 ],
  [ 2, 3 ], [ 2, 3 ],
  [ 2, 3 ], [ 2, 3 ]
]

That’s kind of hard to extract something useful from it.

You can use pythontutor, it runs your code step by step.

VS Code has built-in debugger.

You can write test cases in somewhat ordered fashion, see example, I did this testcases for one of my JS cert projects:

const testCases = {
  
  'onedigit': [[2, 'II'], [3, 'III'], [4, 'IV'], [5, 'V'], [9, 'IX']],
  
  'twodigit': [[12, 'XII'], [16, 'XVI'], [29, 'XXIX'], [44, 'XLIV'], [45, 'XLV']
                , [68, 'LXVIII'], [83, 'LXXXIII'], [97, 'XCVII'], [99, 'XCIX']],
                
  'threedigit': [[400, 'CD'], [500, 'D'], [501, 'DI'], [649, 'DCXLIX'],
                [798, 'DCCXCVIII'], [891, 'DCCCXCI']],
  
  'fourdigit' : [[1000, 'M'], [1004, 'MIV'], [1006, 'MVI'],
                  [1023, 'MXXIII'], [2014, 'MMXIV'], [3999, 'MMMCMXCIX']]
}

for (let group in testCases) {
  console.log('tests for ', group, ' numbers');
  for (let testCase of testCases[group]) {
    console.log('CASE: ', testCase[0]);
    console.log('expected: ', testCase[1]);
    console.log('fact: ', convertToRoman(testCase[0]));
    console.log('----')
  }
  console.log('------------');
}

And one more note:

you named variable just like existing method

maybe it’s just me, but it is super confusing

function chunkArrayInGroups(arr, size) {

  let blankArray = [];
  let blankArray2 = [];
  let multiplier = 1;

  for (let i = 0; i < arr.length; i++) {  
    blankArray.push(arr[i]);  
  }

  let slice1 = blankArray.slice(0,size); // always slices the correct first element
  let slice2 = blankArray.slice(size, size + slice1.length); // always slices the correct 2nd element.
  let slice3 = blankArray.slice( 2 * size,  2 * size + slice1.length); //always slice 3rd element.
  let slice4 = blankArray.slice( 3 * size,  3 * size + slice1.length); //always slice 4th element. So on and so forth adding 1 to the multiplier.
  //console.log(slice);

  console.log(slice1);
  console.log(slice2);
  console.log(slice3);
  console.log(slice4);
  //blankArray.push(slice2); //adds to the back
  //blankArray.unshift(slice2); //adds to the front

  blankArray2.push(slice1);

  let sliceMultiplier = blankArray.slice(multiplier * size, multiplier * size + slice1.length); // always slices the correct 2nd element and so on as multiplier increments.
  for (let i = 0; i < arr.length; i++) {

    if (sliceMultiplier !== [] && multiplier < blankArray.length / size) {  
    console.log(blankArray2.push(sliceMultiplier));
    multiplier++;
    
    }
  }
 
  return blankArray2;
}

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

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

Thanks, pythontutor is a great help. I noticed my multiplier variable did increment, but the code doesn’t care and applies it as if it’s still 1. That’s the part where I’m confused. If that part gets fixed, my code would work.

function chunkArrayInGroups(arr, size) {

  let blankArray = [];
  let blankArray2 = [];
  let multiplier = 1;

  for (let i = 0; i < arr.length; i++) {  
    blankArray.push(arr[i]);  
  }

  let slice1 = blankArray.slice(0,size); // always slices the correct first element
  let slice2 = blankArray.slice(size, size + slice1.length); // always slices the correct 2nd element.
  let slice3 = blankArray.slice( 2 * size,  2 * size + slice1.length); //always slice 3rd element.
  let slice4 = blankArray.slice( 3 * size,  3 * size + slice1.length); //always slice 4th element. So on and so forth adding 1 to the multiplier.
  //console.log(slice);

  console.log(slice1);
  console.log(slice2);
  console.log(slice3);
  console.log(slice4);
  //blankArray.push(slice2); //adds to the back
  //blankArray.unshift(slice2); //adds to the front

  blankArray2.push(slice1);

  let sliceMultiplier = blankArray.slice(multiplier * size, multiplier * size + slice1.length); // always slices the correct 2nd element and so on as multiplier increments.

  function sliceMultiplier2(multiply) {
    return blankArray.slice(multiply * size, multiply * size + slice1.length);
  }

  for (let i = 0; i < arr.length; i++) {

    if (sliceMultiplier !== [] && multiplier < blankArray.length / size) {  
    console.log(blankArray2.push(sliceMultiplier2(multiplier)));
    multiplier++;
    
    }
  }
 
  return blankArray2;
}

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

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));

I found the solution. Can someone explain why the change made the difference?

function chunkArrayInGroups(arr, size) {

  let blankArray1 = [];
  let multiplier = 1;

  let slice1 = arr.slice(0,size); // always slices the correct first element

  blankArray1.push(slice1);

  function sliceMultiplier2(multiply) {
    return arr.slice(multiply * size, multiply * size + slice1.length);
  }

  for (let i = 0; i < arr.length; i++) {

    if (multiplier < arr.length / size) {  
    blankArray1.push(sliceMultiplier2(multiplier));
    multiplier++;
    
    }
  }
 
  return blankArray1;
}

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

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

Cleaned up version of code.

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