Ties with arrays array not working

0

Why does y in second for not "run"?

var i = 0;
    var bd = new Array();
    bd[i++] = new Array(10,11,'Daniel');
    bd[i++] = new Array(12,12,'Augusto');
    bd[i++] = new Array(13, 12, 'Olavo');

Notice that I run the coordinates of this two-dimensional array in x and y - a double loop:

for (x=1 ; x<=i ; x++){
   for(y=0 ; y<=2; y++){
       alert(y);
       if (y=10){ document.write(bd[x][y]); 
       // O Retorno é 10,11,'Daniel'
       }
   }
}

I put an "alert" to see that the value does not go out of zero. Can anyone help me?

    
asked by anonymous 28.08.2017 / 16:46

2 answers

2

Your logic is correct, follow an example with the command push , which serves just to include a record at the end of a array :

/* Inicializa a variável */
// A estrutura final ficará db[linha][coluna]
var db = [];

/* Insere registros */
db.push([1, 2, 'Teste 1']);
db.push([2, 3, 'Teste 2']);
db.push([3, 3, 'Teste 3']);

/* Exibe um registro no console */
console.log(db[1]);

/* Exibe uma célula no console */
console.log(db[2][2]);

/* Varre todos registros e monta uma tabela */

// Loop pelas linhas
for (var iRow = 0; iRow < db.length; iRow++) {
  
  // Aqui criamos a tag <tr>
  var $tr = document.createElement('tr');
  
  // Loope pelas colunas
  for (var iCol = 0; iCol < db[iRow].length; iCol++) {
    
    // Aqui criamos a tag <td>
    var $td = document.createElement('td');
    
    // Aqui pegamos o conteúdo da célula pela linha/coluna e colocamos dentro da tag <td>
    $td.innerHTML = db[iRow][iCol];
    
    // Aqui incluímos a tag <td> dentro da tag <tr>
    $tr.appendChild($td);
  }
  
  // Aqui incluímos a tag <tr> dentro da tag <tbody> da tabela (pelo ID)
  document.getElementById('tabela').appendChild($tr);
}
body {
  font-family: sans-serif;
  font-size: 18px;
}
table td {
  border: 1px solid #ccc;
  padding: 3px 5px;
}
<table>
  <thead>
    <tr>
      <td>ID</td>
      <td>Código</td>
      <td>Nome</td>
    </tr>
  </thead>
  <tbody id="tabela"></tbody>
</table>

As for the problem of your code

This is a syntax error on line for(y=0; y<=i; Y++){ because the "third y" is uppercase. And as in javascript the variable names are case sensitive , that is, the name must be identical including uppercase or lowercase.

The following is the corrected code:

var i = 0;
    var bd = new Array();
    bd[i++] = new Array(10,11,'Daniel');
    bd[i++] = new Array(12,12,'Augusto');
    bd[i++] = new Array(13, 12, 'Olavo');

for (x=1 ; x<=i ; x++){
   for(y=0; y<=i; y++){
       if (y == 10){ document.write(bd[x][y]); 
       // O Retorno é 10,11,'Daniel'
       }
   }
}
    
28.08.2017 / 17:17
1

Your bd array consists of 3 lines numbered 0 through 2. Each row has another array with columns numbered 0 through 2 as well. At the end of the popular part of the array, the value of i will be 3.

Now let's look at your code:

for (x=1 ; x<=i ; x++){
   for(y=0 ; y<=2; y++){
       alert(y);
       if (y=10){ document.write(bd[x][y]); 
       // O Retorno é 10,11,'Daniel'
       }
   }
}

Now, let's look at your problems:

  • In the first for , x goes from 1 to 3. But the lines are 0 to 2!

  • In if , you are using y=10 , not y == 10 . This causes y to change to 10 soon in the first iteration when it was 0. In for , it then passes to y++; and then it evaluates y<=2 , falling out of for . p>

  • Even using y == 10 , since in the second for , y goes from 0 to 2, it will never be 10!

I think what you wanted was this below. Click the blue Run button to see this working.

var i = 0;
var bd = new Array();
bd[i++] = new Array(10, 11, 'Daniel');
bd[i++] = new Array(12, 12, 'Augusto');
bd[i++] = new Array(13, 12, 'Olavo');

document.write('i = ' + i + '<br><br>');

for (var x = 0; x < i; x++) {
   for (var y = 0; y < 3; y++) {
       document.write(bd[x][y] + '<br>');
   }
   document.write('<br>');
}
    
03.09.2017 / 01:24