Files

1.4 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
ab306dbdcc907c7ddfc30830 Usar o rolo compressor 5 16079 steamroller

--description--

Achate um array aninhado. Você deve lidar com diferentes níveis de aninhamento.

--hints--

steamrollArray([[["a"]], [["b"]]]) deve retornar ["a", "b"].

assert.deepEqual(steamrollArray([[['a']], [['b']]]), ['a', 'b']);

steamrollArray([1, [2], [3, [[4]]]]) deve retornar [1, 2, 3, 4].

assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);

steamrollArray([1, [], [3, [[4]]]]) deve retornar [1, 3, 4].

assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);

steamrollArray([1, {}, [3, [[4]]]]) deve retornar [1, {}, 3, 4].

assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);

A solução não deve usar os métodos Array.prototype.flat() ou Array.prototype.flatMap().

assert(!code.match(/\.\s*flat\s*\(/) && !code.match(/\.\s*flatMap\s*\(/));

--seed--

--seed-contents--

function steamrollArray(arr) {
  return arr;
}

steamrollArray([1, [2], [3, [[4]]]]);

--solutions--

function steamrollArray(arr) {
  if (!Array.isArray(arr)) {
    return [arr];
  }
  var out = [];
  arr.forEach(function(e) {
    steamrollArray(e).forEach(function(v) {
      out.push(v);
    });
  });
  return out;
}