function steamrollArray(arr) {
var answer = [];
for(i=-1; i<arr.length; i++){
//empty array case
if(arr.length===0){
return answer;
}
if(i===-1){
i++;
}
//if the current array index does
//not contain another array
if(!Array.isArray(arr[i])){
answer.push(arr[i]);
}
//if the current array has a
//sub-array use recursion
if(Array.isArray(arr[i])){
answer = steamrollArray2(arr[i], answer);
}
}
return answer;
}
//I created a second function just like the first
//because I don't want to re-initialize
//answer = []; and FCC doesn't like global
//variables
function steamrollArray2(arr, answer) {
for(i=-1; i<arr.length; i++){
//empty array case
if(arr.length===0){
return answer;
}
if(i===-1){
i++;
}
//if the current array index does
//not contain another array
if(!Array.isArray(arr[i])){
answer.push(arr[i]);
}
//if the current array has a sub-array
//use recursion to.
if(Array.isArray(arr[i])){
steamrollArray2(arr[i],answer);
}
}
return answer;
}
steamrollArray([1, [2], [3, [[4]]]]);
/*
steamrollArray([[["a"]], [["b"]]]) should return ["a", "b"].
steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4].
steamrollArray([1, [], [3, [[4]]]]) should return [1, 3, 4].
steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4].
*/
// for your loop statements use var i = -1 when initializing the index
// use this instead
for(var i = -1; i < arr.length; i++) { /* your loop code */ }
// otherwise i is a global variable and your recursive functions keep overwriting it
// so after it returns from one of them, it goes back to -1 every time
// also I'm typing up an alternative way to check for array length because looping through an array
// starting at index of -1 is confusing and not clear, therefore it's a bad practice
function steamrollArray(arr) {
var answer = [];
// steamrollArray2 is redundant
// here's how to avoid writing the code twice:
//
// instead we can define a function instead here rather than the global
// namespace and then it avoids writing the same code twice
//
// notice it's doesn't take the answer as an argument
// that's because it will have access to it since it's being defined inside
// the steamrollArray function
var recursiveSteamrollArray = function(arr) {
// starting at index of -1 makes your code unclear, and therefore is
// a bad practice, even though your logic is correct
//
// to avoid the case of arr.length being 0 we can check before enterting
// the for loop
if (arr.length === 0) {
return answer;
}
for (var i = 0; i < arr.length; i++) {
// no need to use if (i === -1) { i++; } anymore
//if the current array index does
//not contain another array
if (!Array.isArray(arr[i])) {
answer.push(arr[i]);
}
//if the current array has a
//sub-array use recursion
if (Array.isArray(arr[i])) {
answer = recursiveSteamrollArray(arr[i], answer);
}
}
return answer;
};
// recursiveSteamrollArray was defined but not called yet
// so let's call the function and then return answer
recursiveSteamrollArray(arr);
return answer;
}
console.log(steamrollArray([1, [2],
[3, [
[4]
]]
]));
function steamrollArray(arr) {
var flatArr = [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
var tempArr = steamrollArray(arr[i]);
for (var j = 0; j < tempArr.length; j++) {
flatArr.push(tempArr[j]);
}
} else {
flatArr.push(arr[i]);
}
}
return flatArr;
}