Chunky monkey operator question

Hey guys, so I have completed the challenge on my own :), my question is, this specific line of code if (xtraArr.at(-1) == 0) at first had a strict equality instead of a normal one like now, and it didn’t work.
Question is, is it because they are both numbers, and the strict makes sure as a default option that they are both different types?
ty.

  **Your code so far**

function chunkArrayInGroups(arr, size) {
/*const xtraArr = [arr.splice(0,size)]; //main idea

xtraArr.push(arr.splice(0,size)); //main idea
console.log(arr)
xtraArr.push(arr.splice(0,size));
console.log(xtraArr)*/
const xtraArr = []

while (arr.length >= size) {
  xtraArr.push(arr.splice(0, size));
  console.log(xtraArr)
}
if (arr.length < size) {
  xtraArr.push([...arr]);
}
if (xtraArr.at(-1) == 0) {
  xtraArr.pop();

}
console.log(xtraArr)
return xtraArr;

}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);
  **Your browser information:**

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

Challenge: Chunky Monkey

Link to the challenge:

Here:

if (arr.length < size) {
  xtraArr.push([...arr]);
}

you’re checking if remaining array is less than required size, but this check also passes if array is empty, so you’re adding an empty array at the end of xtraArr (btw, why are you cloning the array before pushing?).

Then here:

if (xtraArr.at(-1) == 0) {
  xtraArr.pop();
}

you’re checking if the last element of the array, and if in the previous step you added an empty array, this will coerce to true.

So this code kind of works by accident :man_shrugging:
You could add check for the array length in the first if statement and then get rid of the second if altogether.

I guess I can add (arr) the result should be the same. my goal was to push whatever is left after slice.

I mean yeah that was my goal, since it won’t pass with an empty array at the end, and in case there is an empty array I have to remove it so that was my solution.
But still I’m not sure why strict equality wouldn’t pass it.

could you explain this one? I am not sure how to implement it.

Because the last element is an empty array. When you compare an empty array to 0 it’ll get coerced to 0 and that’s why it’ll pass.

Instead of just checking if what’s left is bigger than size you should first check if there’s anything left at all (length is not 0):

if (arr.length && arr.length < size) {
    ^^^^^^^^^^
  xtraArr.push([...arr]);
}

And you really don’t have to check arr.length < size because the array will be either empty or less than size (check your while loop).

if (arr.length !==0) {
  xtraArr.push(arr);
}

thanks this seems to remove the need of my 2nd if statement.
so if anything is empty it just pushes it.

so yeah both of solutions work but I guess this is better because the code is clearer I assume.
I have another question, I read it everywhere that it’s better to write clearer code.
Is it because for “faster website” ? what else are the reasons for it? I 'd assume it’s easier to read for the person also.

Thanks again in advance

“Clean code” is kind of subjective, but yes, it’s so other developers (and future you) can make sense of what’s going on.

2 Likes

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