Arrays array loops do not work

Asked

Viewed 82 times

0

Why the y in the 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');

See 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 come out of zero. Someone can help me?

  • But, what is the question even? It was not clear to me.

  • Could you elaborate on your question?

  • Thank you for asking! I reissued the statement. My code is not working. It gives syntax error. I must be making some serious mistake.

2 answers

2

Your logic is correct, follow an example with the command push which serves precisely 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 your code problem

It’s a syntax error on the line for(y=0; y<=i; Y++){ because the "third y" is capitalized. And as in javascript the variables name are case sensitive, that is, the name needs to be identical including upper or lower case.

Below 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'
       }
   }
}

  • Interesting this code! I think I can use it for another purpose of creating zeroed arrays and transferring strings from one array to the other. Something a little more dynamic. My goal in this code is to do something that a non-programmer user needs to maintain, so it has to be mega easy to view and edit. I will still need to evolve the code to catch the anchovies in the range of D-4 and D+3, but there are another five hundred and the easier part..

1

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

Now let’s see 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 see your problems:

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

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

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

I think what you wanted was this below. Click the blue button Execute 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>');
}

Browser other questions tagged

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