I am currently doing a challenge where we have to reimplement various array methods without using the built in array methods. I've done almost all of them except the concat. With the concat, we have to make sure that it returns a single array with every element from the inputs regarding the parameters.
function newConcat(...item) {
let newArray = [...item];
return newArray;
}
This works for the most part but when it takes in arrays, it keeps them separate so it ends up being an array of arrays instead of just one large array that has been concatenated .
I did it using spread operator, respecting your function parameters definition:
function newConcat(...arrays) {
let result = [];
for(let x = 0; x < arrays.length; x++) {
result = [...result, ...arrays[x]]; // using spread
}
return result;
}
console.log(newConcat([1, 2, 3], [4, 5, 6]));
Not sure if your challenge will accept the spread operator, so there is a more simple code version:
function newConcat(...arrays) {
let result = [];
for(let x = 0; x < arrays.length; x++) {
const item = arrays[x];
for(let y = 0; y < item.length; y++) {
result.push(item[y]);
}
}
return result;
}
console.log(newConcat([1, 2, 3], [4, 5, 6]));