Tell us what’s happening:
So i solved the challenge of the nested arrays but i mostly did it because i searched it on google.I found the command var arr = Array.prototype.concat.apply([], arr);
which actually solved the problem,all the rest code is written by me.So can someone explain me what does this command actually does,i mean i know what concat does but the apply method is the first time i see it and i am bit confused.Even i solved the challenge i don’t wanna have ‘gaps’ on my learning curve.Thanks!
Your code so far
function steamrollArray(arr) {
var arr = Array.prototype.concat.apply([], arr);
let isFlat = false;
for(let i=0;i<arr.length;i++){
if(Array.isArray(arr[i])){
return steamrollArray(arr)
}
}
return arr;
}
console.log(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/85.0.4183.83 Safari/537.36
.
Challenge: Steamroller
Link to the challenge: