Here's the problem.
I have 2 arrays
Arrays :
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
var arr2 = ['up', 'down', 'left', 'right'];
I want to insert the arr2
elemments inside the arr1
every n
step for example :
/*
If the step is 2 the result would be :
*/
[1,2,'up',3,4,'down',5,6,'left',7,8,'right',9, 10, 11, 12, 13, 14, 15, 16];
I have this function as a start point but I can't figure out how to solve my problem from : How to add item in array every x items?
/**
* Add an item to an array at a certain frequency starting at a given index
* @param array arr - the starting array
* @param mixed item - the item to be inserted into the array
* @param integer starting = the index at which to begin inserting
* @param integer frequency - The frequency at which to add the item
*/
function addItemEvery(arr, item, starting, frequency) {
for (var i = 0, a = []; i < arr.length; i++) {
a.push(arr[i]);
if ((i + 1 + starting) % frequency === 0) {
a.push(item);
i++;
if(arr[i]) a.push(arr[i]);
}
}
return a;
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
arr = addItemEvery(arr, "item", 0, 3);
console.log(arr);
Thank you so much for helping