# *Intermediate Algorithm Scripting: Steamroller

Tell us what’s happening:
How may i set to split it with double quotes? I have to finish 3 tasks of the challenge, but I do not know how to set the double quotes. And what may I do in this case`steamrollArray([1, {}, [3, [[4]]]])` should return `[1, {}, 3, 4]` .?

``````
function steamrollArray(arr) {

console.log(arr.join("").split(""))

let b = arr.join().replace(/,/g,"")*1

var digits = b.toString().split('');

var realDigits = digits.map(Number)

console.log(realDigits);

//return realDigits

}

steamrollArray([[["a"]], [["b"]]])

``````

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

Challenge: Steamroller

What you have is undefined behavior. You cannot use JavaScript like that and expect to get consistent results.

1 Like

This challenge can be solved by turning the array into a string and manipulating that string, but it’s really not the way that it’s intended to be solved.

1 Like

What may I do in case of letters and {}?

``````function steamrollArray(arr) {

let a=arr.join()

let b=[]

for(let i=0;i<=a.length;i++){

if(a[i]==1||a[i]==2||a[i]==3||a[i]==4||a[i]=="a"||a[i]=="b"){

b.push(a[i]*1)

}

}

return b;

}

steamrollArray([1, [2], [3, [[4]]]]);``````

How may i convert everything into string to use join? or what way do you suggest for solving it?

I’d use recursion, personally. It’s more natural than regex, to me.

1 Like

What can I try? I was trying to use strings. What would you suggest to do recursion?

You have an array. I’d leave it as an array. Then you can steamroller each element of the main array if they are themselves arrays.

1 Like

I really suggest solving the problem the way they you are intended to.

1 Like

function steamrollArray(arr) {
hoy may I become {} in a number? I am getting that in my array but as a string.

``````let b=[];

let a=JSON.stringify(arr)

let replacedArr=(a.replace(/[\[\]']+/g,''))

for(let i=0;i<=replacedArr.length;i++){

if(replacedArr[i]==1||replacedArr[i]==2||replacedArr[i]==3||replacedArr[i]==4  ){

b.push(replacedArr[i]*1)

}else if(replacedArr[i]=="a"||replacedArr[i]=="b"){

b.push(replacedArr[i])

}else if(replacedArr[i]=="{" ){

b.push(replacedArr[i]+"}")

}

}

console.log(b)

return b

}

steamrollArray([1, {}, [3, [[4]]]]);
``````

Your solution is not going to work because you are trying to hard-code specific values. Your function needs to work for any array.

Converting the array to a string is NOT the intended way to solve this (or a particularly good one), but it can be done using regular expressions.

2 Likes

I am trying to make recursion, aber it is not working.

``````let newArr = [];
const steamrollArray = (arr) => {
for (let x of arr) {
if (Array.isArray(x)) {
steamrollArray(x);
} else {
newArr.push(x);
}
}
};
``````

You have the start of recursion but you aren’t using recursion. You steamroll x if it is an array, but you need to use that steamrolled version of x to update newArr.

Also, newArr should not be a global variable.

2 Likes

As Jeremy said, you shouldn’t have a global variable. You also aren’t returning anything from your `steamrollArray` or doing anything with the value that should be returned by `steamrollArray(x)`.

2 Likes