-2
I need to use the do
and the while
in javascript, but I never did and I’m having some doubts.
I’m using it this way:
$(function () {
$.getJSON('./alertas', function (data) {
var linha = ``;
var nomede = '';
var i = 0;
do {
if(nomede != Data){
linha += `<tr>
<td>${ Data }</td>
</tr>`
}
}
while(i < data){
Id = data[i][0];
De = data[i][1];
Assunto = data[i][2];
Conteudo = data[i][3];
Prioridade = data[i][4];
Hora = data[i][5];
Data = data[i][6];
linha += `<tr >
<td>${ De }</td>
<td>${ Assunto }</td>
<td>${ Prioridade }</td>
<td>${ Hora }</td>
</tr>`;
}
$("#alerta tbody").html(linha);
});
});
But returns this error:
Uncaught Referenceerror: Data is not defined at Object.Success
I’m using the do
and the while
to write a dynamic table and at the same time put a date separation of the returned lines.
Example:
De Assunto Prioridade Recebido
Data: 2020-07-20
1 linha
2 linha
3 linha
4 linha
Data: 2020-07-24
5 linha
6 linha
7 linha
8 linha
and so on
I no longer know how to ask the questions so as not to have negative votes. I try to explain my problem in the best way, but it is always poorly received
– Bruno
The correct syntax is
do { faz algo } while (condição);
but you diddo { faz algo } while (condição) { faz outra coisa }
, that I don’t even know if it’s valid (and if it is, it probably doesn’t do what you need). Anyway, just usedo { faz algo } while (condição);
or justwhile (condição) { faz algo }
- the difference between them has already been explained in the answer below– hkotsubo
@hkotsubo we can talk in chat?
– Bruno
Unfortunately I will not be able, already "closed" for today (I turned off the computer, only I stopped the site by mobile to see if there was any news, but soon I’ll be offline and only return tomorrow) :-) But taking advantage, I saw that this syntax does not give error, but it does not do well what you want: https://ideone.com/b1gbus - I also suggest that you edit the question and put an example of how is this array
data
, because if it’s the way I imagine it, there’s probably another way to solve it (but it’s just speculation, having an example of the data gives you a better idea)– hkotsubo