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?

rene
  • 41,474
  • 78
  • 114
  • 152
John Paine
  • 53
  • 8
  • 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. – Yoshi Jan 28 '16 at 07:44
  • Arrays are objects. They are passed by reference so `var a = ['a']; var b = ['a']; a==b` will return false. – Rajesh Jan 28 '16 at 07:44
  • 1
    JSON.stringify works, thank you, @Yoshi! I forgot that arrays are objects =( – John Paine Jan 28 '16 at 07:49

2 Answers2

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?

Community
  • 1
  • 1
P. Jairaj
  • 1,033
  • 1
  • 6
  • 8
  • 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); ` – John Paine Jan 28 '16 at 08:07
  • Yeah, thank you, @P. Jairaj! But if we'd be a bit more meticulous, second length check I think should be like this: element.length == array2[index].length Because inner array may have different length =) – John Paine Jan 28 '16 at 08:20
  • Arghhh... I'm tired of these comment code formating... Can anyone help me with it? =) I've tried all the variants I think... – John Paine Jan 28 '16 at 08:23
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);
John Slegers
  • 45,213
  • 22
  • 199
  • 169