I get error from Your solution should not use the Array.prototype.flat() or Array.prototype.flatMap() methods

Tell us what’s happening:

Intermediate Algorithm Scripting: Steamroller

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

Your code so far


function steamrollArray(arr) {
return arr.flat(Infinity);
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Steamroller

Link to the challenge:

Welcome, bhautik.

Do you understand what the error is saying?

@bhautik_domz

You are using the Array.prototype.flat() method.

return arr.flat(Infinity);

arr is an instance of the Array object which you can verify by running the following code:

let arr = [];
console.log(arr instanceof Array);
console.log(arr instanceof Object);
console.log(arr instanceof String);
console.log(arr instanceof Boolean);
console.log(arr instanceof Number);

Array.prototype (you will learn this later in the exercises) is the series of methods that every array inherits from the Array class constructor.

I want to say that Array is the parent class of arr but I don’t know enough about js to know if that is technically true.

You’ll note that arr is also an Object which means it could also take advantage of any methods in the Object prototype, like Object.prototype.getOwnPropertyNames().

You can verify this your self by running the method on your array:

console.log(Object.getOwnPropertyNames(arr));

This is a very long-winded way of telling you that you are doing something the assignment specifically asks you not to do.

You are using a built-in method to solve the problem but the exercise asks you to build your own custom function to flatten the array in an effort to understand what is happening behind the scenes on this method that can be executed on any instance of an Array.

1 Like

Question :-
Intermediate Algorithm Scripting: Steamroller
Flatten a nested array. You must account for varying levels of nesting.

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

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

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

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

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

I use this Code :-
function steamrollArray(arr) {
arr=arr.flat(Infinity);
return arr;
}

console.log(steamrollArray([1, [2], [3, [[4]]]]));

Challange link :- https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller

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

Your solution:

function steamrollArray(arr) {
  arr = arr.flat(Infinity);
  return arr;
}

You are using the Array.prototype.flat() method.
You are not allowed to use that method to solve this problem.