1

I solve challenges on FreeCodeCamp. Code below passes their tests and challenge marked as resolved but my own tests don't pass and I just don't get why...

function chunk(arr, size) {
    if (size >= arr.length || size <= 0)
        return [arr];
    var result = [];
    var i = 0;
    while(arr.length > 0) {
        result.push([]);
        for (var j = 0; j < size && arr.length > 0; ++j) {
            result[i].push(arr.shift());
        }
        i++;
    }
    return result;
}

alert(chunk(["a", "b", "c", "d"], 2) == [["a", "b"], ["c", "d"]]);

Alert should print true if I got the essense of arrays in JS right, but it prints false and I don't know why?

3
  • As array are objects, and objects are only equal if both point to the same object in memory, you can't check arrays for equality by simply using ==. You'll need a more sophisticated equality-check. Though, for such simple cases, comparing JSON.stringify results could work. Commented Jan 28, 2016 at 7:44
  • Arrays are objects. They are passed by reference so var a = ['a']; var b = ['a']; a==b will return false. Commented Jan 28, 2016 at 7:44
  • 1
    JSON.stringify works, thank you, @Yoshi! I forgot that arrays are objects =( Commented Jan 28, 2016 at 7:49

2 Answers 2

1

This is working:

function chunk(arr, size) {
  if (size >= arr.length || size <= 0)
    return [arr];
  var result = [];
  var i = 0;
  while (arr.length > 0) {
    result.push([]);
    for (var j = 0; j < size && arr.length > 0; ++j) {
      result[i].push(arr.shift());
    }
    i++;
  }
  return result;
}
var array1 = chunk(["a", "b", "c", "d"], 2);
var array2 = [
  ["a", "b"],
  ["c", "d"]
];
var equals = (array1.length == array2.length) && array1.every(function(element, index) {
  return (element.length == array2[index].length) && element.every(function(element1, index1) {
    return element1 === array2[index][index1];
  });
});
alert(equals);

More info: How to Compare two Arrays are Equal using Javascript?

Sign up to request clarification or add additional context in comments.

3 Comments

It didn't work, as I think, because these arrays are 2 dimensional. Please look at my corrections and give some feedback if my checkings are correct? ` var equals = (array1.length == array2.length) && array1.every(function(element, index) { return element.every(function(element2, index2) { return element2 == array2[index][index2]; }); }); alert(equals); `
Yeah, thank you, @P. Jairaj! But if we'd be a bit more meticulous, second length check I think should be like this: <code> element.length == array2[index].length </code> Because inner array may have different length =)
Arghhh... I'm tired of these comment code formating... Can anyone help me with it? =) I've tried all the variants I think...
0

This is the most efficient solution :

function chunk(arr, size) {
    var output = [];
    var length = arr.length;
    for (var x = 0, length = arr.length; x < length; x = x + size) {
        output.push(arr.slice([x], x + size));
    }
    return output;
}

var array1 = chunk(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M'], 2);

var array2 = [
    ["A", "B"],
    ["C", "D"],
    ["E", "F"],
    ["G", "H"],
    ["J", "K"],
    ["L", "M"]
]

var equals = (array1.length == array2.length) && array1.every(function(element, index) {
  return (element.length == array2[index].length) && element.every(function(element1, index1) {
    return element1 === array2[index][index1];
  });
});
alert(equals);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.