"Steamroller" challenge - {} returning "object"

Hi!

I wrote this code to solve the “Steamroller” challenge and it worked for 3 of the 4 items on the checklist. The problem here is that {} are returning the string object (one for each) divided into letters. I checked if the problem was the .split(/ \ W /), but it is not. Is it possible to fix this?

  **This is my code so far**

function steamrollArray(arr) {
let result = arr.join("").split(/\W/).join("").split("");
return result.map(a => {
  if (!Number.isInteger(parseFloat(a))) {
    return a;
  } else if (Number.isInteger(parseFloat(a))) {
    return parseFloat(a);
    } 
});
}

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

// [ 'a', 'o', 'b', 'j', 'e', 'c', 't', 'O', 'b', 'j', 'e', 'c', 't', 3, 4 ]
  **Your browser information:**

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

Challenge: Steamroller

Link to the challenge:

You are going to need to rethink your approach. There is no need to be converting the array to a sting and trying to figure out a way to split the string such that you can handle all the possibilities of values of a given array’s elements.

Step back and think about the steps needed (forget code for a bit) to take a multi-dimensional array and convert it into a one-dimensional array.

1 Like

Thanks for answering.
I finished studying for today.
I’m going to take a look at it tomorrow morning and see what comes to mind.
“Reducing” the dimension of an array is something I still don’t feel comfortable doing. I always try to improve my code (and change it, to see the effect and see if something different would return the same thing) after finishing a challenge, but I’m still having problems with it.
As soon as I finish working on that, I’ll post the results.

Hi!
Yes, I’m stuck. I took a look at the challenge guide. I’ve never seen this Array.isArray () method. before I took a brief look at the solutions and I will skip this challenge for now, but I will repeat it later, trying to apply the Array.isArray () method. I’ve done this before and it’s been good.
My idea was to do something like solution 3. The only thing that worries me is that this part
if (v == "[object Object]") { // bring back empty objects return {};
only works because this situation is known. If it were [], for example, it wouldn’t work.

function steamrollArray(arr) {
  return arr
    .toString()
    .replace(",,", ",") // "1,2,,3" => "1,2,3"
    .split(",") // ['1','2','3']
    .map(function(v) {
      if (v == "[object Object]") {
        // bring back empty objects
        return {};
      } else if (isNaN(v)) {
        // if not a number (string)
        return v;
      } else {
        return parseInt(v); // if a number in a string, convert it
      }
    });
}

Oh, and by the way, the first solution shows an error.
This part

  // Call the function for each element in the array
  arr.forEach(flatten);
  return flattenedArray;
}

is out of the function. After removing it, it works.

Thank you so much for your attention @camperextraordinaire! It’s great to have a feedback on my approach. I’m new to this area (I have a bachelor’s degree in physical education / sports science), so this kind of feedback is extremely important to me.
Thanks! Have a nice weekend!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.