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?
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.
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!