I need to [1, 2, 3] over to ['123']. I must return [1, 2, 3] over to ['123'] while using an arrow function (no regex):
Must be used:
const functionOne = (arrayOne) => {
};
console.log(functionOne([1, 2, 3]));
So, I tried the following:
First, I created a string. This gave me 1,2,3
Then, I removed the commas, so, I could join the numbers. This gave me 123.
Finally, I tried to put back the number as a string into the array but this didn't work. This gave me ['1', '2', '3']
instead of ['123']
. I think the .split
method is what is wrong in my code but I cannot find another one (currently learning JavaScript).
const functionOne = (arrayOne) => {
let stepOne = arrayOne.toString(arrayOne => arrayOne.toString());
console.log(stepOne);
stepOne = stepOne.split(',').join('');
console.log(stepOne);
return stepOne.split('');
};
console.log(functionOne([1, 2, 3]));
You can join
using ""
as the delimiter (it will automatically convert the elements to strings as it produces the output string), then put the result in an array as its only element ([____]
):
const functionOne = (array) => [array.join("")];
console.log(functionOne([1, 2, 3]));
Issues with your original approach:
toString
completesly ignores the argument you're giving it, instead using its default behavior of join(",")
.,
again just gives you back an array, but this time of strings.join('')
does give you "123"
, but doesn't put it in an array.