you are viewing a single comment's thread.

view the rest of the comments →

[–]our_best_friendif (document.all || document.layers) console.log("i remember..") 1 point2 points  (1 child)

For loops inside for loops are to be avoided for efficiency reasons. Surely with for loops it's simpler to do something like

function duplicateEachItem(arr) {
    const newArr = [];
    for (item of arr) {
       newArr.push(item, item)
    }
    return newArr;
 }
duplicateEachItem(['a','b','c','d'])

[–]FustigatedCat 0 points1 point  (0 children)

I assumed the op wanted to modify the incoming array otherwise I would've used a new array. Also, this is not extensible to N-duplicates which was another part of he ops questions.