I have this function:
function insertValue2(cantidad, value, arr){
var spaceCount = 0;
arr.forEach(function(item, index) {
if(item == "") {
console.log(index)
spacecount++;
}
});
console.log(spaceCount)
My function counts blank spaces in this array : ["", "", "", "B", "B", "B", ""]
So the result for this: 0 1 2 6 are the positions in the array with blank spaces and spacecount = 4
I dont know if this is possible but any idea how to count blank space before i get the first B??
I mean count for consecutive blank spaces like 0 1 2 spacecount = 3 and then count space for 6 spacecount = 1
And if i want to insert in my array quantity = 1 with a value = C that will choose the lowest value for spacecount.
["", "", "", "B", "B", "B", "C"]
EDIT:
Quantity is how many space i will use in the array, i dont want to use more space than the necessary for the quantity
In this array i have blank space in positions 0, 1, 2, 7 , 8, 9, 10
["", "", "", "B", "B", "B", "C", "" , "" ,"" ,""]
if i want to insert quantity = 2 and value = D, the expected result is:
["D", "D", "", "B", "B", "B", "C", "" , "" ,"" ,""]
and if i want to insert quantity = 1 and value = "E" it will choose the position 2 with the spacecount = 1 to save the
higher spacecount for a bigger quantity like 3 or 4
["D", "D", "E", "B", "B", "B", "C", "" , "" ,"" ,""]
Thanks for the help :)
0 1 2 6are the positions in the array with blank spaces andspacecount = 4i dont know what you dont understand :Sspacecountfor consecutive positions and then i will insert my values in the positions with the lowestspacecount