Intermediate Scripting: Steamroller - flatten array

my code works when I check with
console.log(steamrollArray([1, {}, [3, [[4]]]])); and others
I get back correct answer from console.log
but I automated marking says NO!!!

code that I believe is working:

function steamrollArray(arr) {
  
  if ((Array.isArray(arr) && arr.length)) {
    for (let i=0; i<arr.length; i++) { 
      if ((Array.isArray(arr[i]) && arr[i].length)) {
        steamrollArray(arr[i]);
      } else { x.push(arr[i]);}
    }
  }
  return x;
}
var x = []; 
console.log(steamrollArray([1, {}, [3, [[4]]]]));

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

yes my code is not totally efficient. As there is a recursive function, I need the var global so that it keeps adding to the global array.
the problem is that when I run it, I get the correct answers that it is supposed to give. This is an exercise from the javascript intermdediate scripting lessons.
My solution works but I cant progress in the lessons because the automated marking says it does not work

add a second function call and see if that one gives the expected result - that’s your issue


console.log(steamrollArray([1, {}, [3, [[4]]]]));
console.log(steamrollArray([1, 2, [3], [4,5,[6]]]))

the second one gives result of [1, {}, 3, 4, 1, 2, 3, 4, 5, 6], which is wrong


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

was not in the requirements…but changing it slightly and almost there:

function iterate(y,arr) {
  if ((Array.isArray(arr) && arr.length)) {
    for (let i=0; i<arr.length; i++) { 
      if ((Array.isArray(arr[i]) && arr[i].length)) {
        iterate(y,arr[i]);
      } else { y.push(arr[i]);}
    }
  }
  return y;
}

function steamrollArray(arr) {
  var x = []; 
  return iterate(x,arr);

}

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

but not the is not an item in the array but denotes an empty array LOL

it’s not an explicit requirement, but the tests call functions, after the function in your code execute

that’s why it wasn’t working: the function call in the editor fill the x array, then the function call from the tests get wrong results because it starts with an x array that is not empty

2 Likes