Two-dimensional Array

4

I'd like to loop through a two-dimensional array according to the code:

var cadeiras = [[true, false, true, true, true, false, false, true, true, true, false, false], 
               [true, false, true, true, true, false, false, true, true, true, false, false], 
               [true, false, true, true, true, false, false, true, true, true, false, false], 
               [true, false, true, true, true, false, false, true, true, true, false, false]];

for (i = 0, i < cadeiras.length, i++){
for (j = 0, j < cadeiras[i].length; j++) {
    if([i][j]){
    var reserva = confirm("A cadeira " + (j+1) + " na fila " + (i+1) + " está disponível. Deseja reservá-la?");
        if(reserva){
            document.getElementById("assento" + numeroCadeira).src = "img/cadeira-tulipa-reservada.jpg"
            document.getElementById("assento" + numeroCadeira).alt = "reservada"
            document.getElementById(numeroCadeira).classList.remove("panel-default");
            document.getElementById(numeroCadeira).classList.add("panel-success");
            alert("A cadeira " + (j+1) + " na fila " + (i+1) + " está reservada.");
            break;
        }
    }   
}

}

The purpose is to check if the chair is free and if it is free give the user the option to accept it. If it is accepted, the loop should be interrupted.

Thanks in advance for the help.

    
asked by anonymous 20.10.2015 / 19:30

4 answers

1

Situation

So I noticed this is a routine that you will call several times, ie you should update the value of the chairs.

Problems

  • As already indicated by the other answers, you are using , instead of ; in the for .
  • Your check is if([i][j]) , I do not know which array it is comparing here, I would say a random one.
  • You are not adjusting the value of the chair.
  • To exit an iteration in javascript it uses return not break .

Code

var cadeiras = [
    [true, false, true, true, true, false, false, true, true, true, false, false],
    [true, false, true, true, true, false, false, true, true, true, false, false], 
    [true, false, true, true, true, false, false, true, true, true, false, false], 
    [true, false, true, true, true, false, false, true, true, true, false, false]
];

function reservar(){
    for (i = 0; i < cadeiras.length; i++){
        for (j = 0; j < cadeiras[i].length; j++) {
            if(cadeiras[i][j]){
                if(confirm("A cadeira " + (j+1) + " na fila " + (i+1) + " está disponível. Deseja reservá-la?")){
                    console.log("A cadeira " + (j+1) + " na fila " + (i+1) + " foi reservada");
                    cadeiras[i][j] = false;
                    return;
                }
            }   
        }
    }
}
reservar();
reservar();

OBS

I would do in an encapsulated function, not to use global variable.

    
20.10.2015 / 20:34
3

First, correct the syntax of for by replacing , with ;

Then, to exit both loops at the same time, break will not resolve. Encapsulate everything within a function , and when reserving the chair, call return to exit both loops.

function reservarCadeiraDisponivel() {
    for (i = 0; i < cadeiras.length; i++) {
        for (j = 0; j < cadeiras[i].length; j++) {
            if ([i][j]) {
                var reserva = confirm("A cadeira " + (j + 1) + " na fila " + (i + 1) + " está disponível. Deseja reservá-la?");
                if (reserva) {
                    document.getElementById("assento" + numeroCadeira).src = "img/cadeira-tulipa-reservada.jpg"
                    document.getElementById("assento" + numeroCadeira).alt = "reservada"
                    document.getElementById(numeroCadeira).classList.remove("panel-default");
                    document.getElementById(numeroCadeira).classList.add("panel-success");
                    alert("A cadeira " + (j + 1) + " na fila " + (i + 1) + " está reservada.");
                    return;
                }
            }
        }
    }

  alert("Nenhuma cadeira disponível");
  return;
}
    
20.10.2015 / 20:00
2

The syntax of your for is wrong, if you use ; and not , , I made an example that will return all the values, within if just put the action if the chair is vague:

var cadeiras = [
  [true, false, true],
  [false, false],
  [true]
];

for (i = 0; i < cadeiras.length; i++) {
  for (j = 0; j < cadeiras[i].length; j++) {
    console.log(cadeiras[i][j]);
    if (cadeiras[i][j]) {
      var reserva = confirm("A cadeira " + (j + 1) + " na fila " + (i + 1) + " está disponível. Deseja reservá-la?");
      if (reserva) {
        // faça algo se for reservado.
        return false; // saia do loop.
      }
    }
  }
}

See working at: JsFiddle

    
20.10.2015 / 19:50
1

It seems like something very simple, here is an example:

Consider that an array traverses the elements through an axis:

+------------------> x
|[true, false, ...]
|[true, false, ...]
|[true, false, ...] 
v
y

To go through javascript, just do something like this, and store the data in an object.

 var cadeiras = [
                 [true, false, true, true, true, false, false, true, true, true, false, false], 
                 [true, false, true, true, true, false, false, true, true, true, false, false], 
                 [true, false, true, true, true, false, false, true, true, true, false, false], 
                 [true, false, true, true, true, false, false, true, true, true, false, false]
                ];
     var free = {"acesso":false};
    for (var y in cadeiras) {
          var x = 0;
          //percorre enquanto for indisponível
          while (cadeiras[y][x] === false) {
            x++; 
          } 
           //sai quando achar o disponível
           free = {"acesso":cadeiras[y][x], "x":x, "y": y}; 
          break;
    }
console.log(free); //a saída será: {acesso: true, x: 0, y: "0"}

Then just display the object:

alert("A cadeira "+free.x+" da fila "+free.y+" está "+((free.acesso) ? 'livre' : 'indisponível')+"!");
    
20.10.2015 / 20:26