Two-dimensional Array

Asked

Viewed 356 times

4

I would like to go 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 goal is to check if the chair is free and if the user is free to accept it. If accepted, the loop must be stopped.

Thanks for your help.

  • 1

    And what’s the problem? Apparently the code is already doing that job.

  • The code, as it was, was not running because of the semicolon and break problems that were not being used correctly. Thank you for your comment.

  • I hadn’t noticed. You’re welcome, I just needed to call.

4 answers

3

First, fix the syntax of for replacing , for ;

Then, to exit both loops at the same time, the break will not solve. Encapsulate everything within a Function, and when booking 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;
}

2

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

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 on: Jsfiddle

  • Perfect Gabriel. Thank you very much for the answer.

1

It seems to me something very simple, follow an example:

Consider that a matrix travels through the elements through an Axis:

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

To browse 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')+"!");

1


Situation

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

Problems

  • As already pointed out by the other answers, you are using , in place of ; in the dives of for.
  • Your check this if([i][j]), do not know which array he is comparing here, would say a random data.
  • you are not adjusting the value of the chair.
  • To quit a javascript iteration is used return nay 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 a encapsulated function, not to use global variable.

  • 1

    Great William. Thank you very much for the answer. Actually this is just a chunk of larger code. Everything is in an IIFE. But still thanks for the tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.