Files
abe 4ab1dd8926 fix(curriculum): recursion challenges that abuse global space (#47680)
* fix: recursion challenges that abuse global space #43516

* fix challenge phrasing and create separate test

* fix: challenges which pollute/impure functions should not pass

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register.md

allow users to ab(use) global space in cash register

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>

* Update curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-roman-numeral-converter-project/roman-numeral-converter.md

allow users to abuse global space in roman numeral converter

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>

Co-authored-by: kravmaguy <flex4lease@gmail.com>
Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
2022-10-12 15:00:24 -05:00

1.5 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
ab306dbdcc907c7ddfc30830 Steamroller 1 16079 steamroller

--description--

Flatten a nested array. You must account for varying levels of nesting.

--hints--

steamrollArray([[["a"]], [["b"]]]) should return ["a", "b"].

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

steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4].

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

steamrollArray([1, [], [3, [[4]]]]) should return [1, 3, 4].

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

steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4].

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

Your solution should not use the Array.prototype.flat() or Array.prototype.flatMap() methods.

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

Global variables should not be used.

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

--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;
}