Test Cases # 4, 5, 6 and 7 in Implement Chunky Monkey algorithm inaccurate?

Test case #4, 5, 6 and 7 in “Implement the chunky Monkey Algorithm” passes with following code snippet. I believe, they should not pass (not passing in IDE). Not sure If I am missing anything

function chunkArrayInGroups(arr, num) {

let iterations = arr.length/num;

let results = \[\];

for ( let i = 0; i < iterations; i++) {

results.push(arr.slice(num \* i, num \* ( i + 1 )));

    }

return results;

}

Though, I later adjusted the code a bit before submission.

function chunkArrayInGroups(arr, num) {
let iterations = arr.length/num;
let results = [ ];
for ( let i = 0; i < iterations; i++) {
if ( ! (num \* ( i + 1 ) in arr)) {
results.push(arr.slice(num \* i));
}
else {
results.push(arr.slice(num \* i, num \* ( i + 1 )));
}
}
return results;
}

Hi.

Can you post the url of the challenge you are doing please.

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

@Ray13 . Thanks for formatting.. Now, I learned that three backticks can be used for codeblocks. Also realized that I incorrectly tried to use “preformatted text” in my initial post. Thanks again.

1 Like

hi @a1legalfreelance Here you go: https://www.freecodecamp.org/learn/javascript-v9/lab-chunky-monkey/implement-the-chunky-monkey-algorithm

Hi @a1legalfreelance (and everybody who bothered to look into this post). I now realized that all the test cases are indeed correct so this post can be safely invalidated. I figured out why my first iteration is working.(everywhere)

function chunkArrayInGroups(arr, num) {
    let iterations = arr.length/num;
    let results = [];
    for ( let i = 0; i < iterations; i++) {
        results.push(arr.slice(num * i, num * ( i + 1 )));
    }
    return results;
}

In my 2nd iteration, I added a condition (though complex which could have been made simpler when I revisit the code 2nd time) which checks the edge condition when the length of array divided by the num does not leads to clean division such as ([1,2,3], 2). However, later discovered that checking that condition is not necessary because javascript string slice automatically handles such cases.

Would be more careful when posting next time. happens sometimes :slight_smile:

1 Like