2

First I am creating an array with an specific size.

$("#bntSize").one('click', function(e){
          var memoria = $("#memoria").val();
          console.log(memoria)

          html = "";

          for(var i = 0; i < memoria ; i++){
               html += "<div class='square' name='mem"+i+"' data-id='"+i+"' data-pos='"+i+"' >"+i+"</div>";
               arrayMemoria.push('');
          }
          console.log(arrayMemoria)
          $("#contenedor").html(html);

        });

If memoria is equal to 7 I am getting this:

["", "", "", "", "", "", ""]

Now I am giving some values to the array:

var nada = 0;

function firstFit(){

          var cantidad = $("#ffinput").val();
          var value = $("#ffinput2").val();
          /*console.log(cantidad)*/

         if(nada == 0){
            for (nada ; nada < cantidad ; nada++) {
                arrayMemoria.splice(nada , 1 , value);
                nada = nada;
            }
         }
         else{

            for (nada; nada < arrayMemoria.length ; nada++) {
                arrayMemoria.splice(nada , 1 , value);
                nada = nada;
            }
         }

Here cantidad: how many spaces I am suppose to use in array & value: just a value.

So if I put => cantidad = 3 and value = A

["A", "A", "A", "", "", "", ""]

Then if I want to put => cantidad = 2 and value = B

["A", "A", "A", "B", "B", "B", "B"]

But I am trying to get this:

["A", "A", "A", "B", "B", "", ""]

and if I put => cantidad = 1 and value = C

["A", "A", "A", "B", "B", "C", ""]

And my second problem

If I do remove the values equals to A and I am inserting => cantidad = 2 AND VALUE = D I am suppose to get this:

["D", "D", "", "B", "B", "C", ""]

How to count the available space in my array? cause if I want to insert

cantidad = 1 and value = E , I need to get the first available space

["D", "D", "E", "B", "B", "C", ""]

If someone can help me please!!

3 Answers 3

1

You can try the following code

	var arr = ["", "", "", "", "", "", ""];
    arr = insertValue(3, "A", arr);
    console.log(arr);
    arr = insertValue(2, "B", arr);
    console.log(arr);
    arr = insertValue(1, "C", arr);
    console.log(arr)
    arr = removeValue("A", arr);
    console.log(arr)
    arr = insertValue(2, "D", arr);
    console.log(arr)
    
    
	function insertValue(cantidad, value, arr){
		var arrLength = arr.length;
		var count = 0;
		for (var i = 0; i < arrLength; i++) {
			if(arr[i] == "" && count < cantidad){
				arr[i] = value;
				count ++;
			}
		};
      return arr;
	}

	function removeValue(value, arr){
		var arrLength = arr.length;
		for (var i = 0; i < arrLength; i++) {
			if(arr[i] == value){
				arr[i] = "";
			}
		};
      return arr;
	}

EDIT: To get the number of spaces in the array

var arr = ["A", "A", " " , " ", " " , "B" ,"C " , " "];
var  spaceCount = 0; 

arr.forEach(function(i) { if(i == " ") spaceCount++;  });
console.log(spaceCount)

EDIT 2: To count consecutive spaces in an array

var arr = ["A", "A", " " , " ", " " , "B"," ", " " ,"C " , " "];
var count = 1;
var countArr = [];

for (var i = 0; i < arr.length; i++) {
	if(arr[i] == " "){
		if(arr[i+1] == arr[i]){
			count ++;
		}else {
			countArr.push(count);
			count = 1;
		}
	}
};

console.log(countArr)

EDIT 3: To get consecutive space count + starting position

var arr = [" ", "A", "A", " " , " ", " " , "B"," ", " " ,"C " , " "];
var count = 1;
var countArr = [];

var pos = 0;

for (var i = 0; i < arr.length; i++) {
	if(arr[i] == " "){
		if(arr[i] === arr[i+1]){
			count ++;
		}else {
			countArr.push({'pos': pos, 'count': count});
			count = 1;
		}
	}else{
		pos = i+1;
	}
};

console.log(countArr)

Sign up to request clarification or add additional context in comments.

3 Comments

can you help me with one more problem? what happen if i have this array ["A", "A", " " , " ", "B", " " , " " , " "] and i want to insert cantidad = 1 , and the expected result will be ["A", "A", " " , " ", " " , "B" ,"C " , " "] and if i want to insert cantidad = 3 ["A", "A", "D" , "D ", "D", "B", " C", " "] , how i can count exactly how many blank space are in the array and put the value in the best position to save space for a bigger cantidad
Any way to count consecutive spaces? i mean : count the first 3 spaces: count = 3 and then count = 1 for the last space, so in that way i will know that i have to insert the value in the last space.
@Eliott I have added the code for consecutive spaces in an array
1

  var array = ["A", "", "", "", "B", "B", "B"];

  var cantidad = 2;
  for (var x = 0; x < array.length; x++) {
    if (array[x] === "") {
      if (cantidad >0){
        array.splice(x, 1, "C");
        cantidad--;
      }
    }
  }

  function codeAddress() {
    alert(array);
  }

  window.onload = codeAddress;

Here's a solution, I realized you can solve this problem in a lot of different ways, mine is not necessarily the best approach.

Good luck.

Comments

0

EDIT: This is a working solution for both questions.

var array = ["","","","","","",""];

function addValues(num, value) {
    for(var i=0; i<num; i++) {
        for(var j=0; j<array.length; j++){
            if(array[j] ==="") {
                array[j] = value;
                break;
            }
        }
    }
}

function removeValues(value) {
    for(var i=0; i<array.length; i++) {
        if(array.indexOf(value) !== -1) {
            array[i] = "";
        }
    }

}
addValues(3, 'A');
addValues(2, 'B');
addValues(1, 'C');

removeValues('A');

addValues(2, 'D');
addValues(2, 'E');

console.log(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.