[KNOWN] JS Iterators - Codeacademy editor won't accept .flat() method on a nested array

Topic

Iterators — Review in the Learn JavaScript Syntax: Iterators module.
I’m working on Safari 14.0, Catalina 10.15.7.

Summary of the report:

I was trying some things out in the Codeacademy editor, working through the ‘Challenge Yourself’ section at the end. I tried using the .flat() method on a nested array, but it wouldn’t work. It did work in my own editor. :thinking:

Course URL: https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-iterators/lessons/javascript-iterators/exercises/iterators-review

Steps to Reproduce:
My code was as follows:

const arr = [2, 4, 6, 8, 10];
const nestedArr = ["a", "b", "c", ["x", "y", "z"]];

// Callback function
const timesTwo = (num) => num * 2;

// forEach
arr.forEach((number) => console.log(timesTwo(number)));

// map
console.log(arr.map((number) => timesTwo(number)));

// Chaining iterators (map, then filter)
console.log(arr.map((number) => timesTwo(number)).filter((num) => num > 10));

const singleLayer = nestedArr
  .reduce((acc, curr) => acc + curr)
  .split("")
  .filter((element) => element !== ",");

console.log(singleLayer);

console.log(nestedArr.flat());

The output is:

4
8
12
16
20
[ 4, 8, 12, 16, 20 ]
[ 12, 16, 20 ]
[ 'a', 'b', 'c', 'x', 'y', 'z' ]
/home/ccuser/workspace/javascript-iterators-iterators-review/main.js:21
console.log(nestedArr.flat());
                      ^

TypeError: nestedArr.flat is not a function
    at Object.<anonymous> (/home/ccuser/workspace/javascript-iterators-iterators-review/main.js:21:23)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

Fix or Workaround: Unknown

Please attach screenshots

Hi, welcome to the forums.

I believe the issue is related to the fact that the exercise in question is using an old version of Node:

As we can see from the Mozilla dev docs “Browser Compatibility” section for the Array.prototype.flat() method, Node.js only began supporting the use of this method with version 11. :slight_smile:

1 Like

Ah, makes sense — thank you, @thepitycoder! :clap:

1 Like