I'm trying to create a function that duplicates every 5 found in the provided array by using Array.prototype.forEach() and Array.prototype.splice():
function duplicateFives(array) {
function multiplyFives (element, index) {
if (element === 5) {
array.splice(index, 0, 5);
index++;
}
}
array.forEach(multiplyFives);
return array;
}
However, it seems modifying the second parameter of the .forEach method doesn't skip iterations (elements). Is this correct, or am I making a different mistake?
Mdn does not seem to confirm nor specify any functionality related to the index parameter.