Overview
Implement concat(...args) that merges arrays and values into a new array, like Array.prototype.concat.
Examples
concat([1,2], [3,4], 5); // [1,2,3,4,5]
Solution
Reveal solution
function concat(...args) {
const result = [];
for (const arg of args) {
if (Array.isArray(arg)) result.push(...arg);
else result.push(arg);
}
return result;
}array-prototype-concat.js
Array.prototype.concat
easycodingJavaScriptArrays
Overview
Implement concat(...args) that merges arrays and values into a new array, like Array.prototype.concat.
Examples
concat([1,2], [3,4], 5); // [1,2,3,4,5]
Solution
Reveal solution
function concat(...args) {
const result = [];
for (const arg of args) {
if (Array.isArray(arg)) result.push(...arg);
else result.push(arg);
}
return result;
}NameTopicDifficulty
103 of 103 problems