How to use while in javascript

Asked

Viewed 117 times

-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

  • 1

    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

  • The correct syntax is do { faz algo } while (condição); but you did do { 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 use do { faz algo } while (condição); or just while (condição) { faz algo } - the difference between them has already been explained in the answer below

  • @hkotsubo we can talk in chat?

  • 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)

2 answers

2


The way you’re using do...while is wrong. The syntax is just:

do {
    faz algo
} while (condição);

But you did:

do {
    faz algo
} while (condição) {
    faz outra coisa
}

However, this block after the while is no longer part of loop. Ex:

var i = 0;
do {
  console.log('dentro', i);
  i++;
  if (i > 2)
    break;
} while (i < 10) {
  console.log('depois', i);
}

The exit is:

dentro 0
dentro 1
dentro 2
depois 3

That is, what is inside the do...while is executed several times, and what is after the while, only once. Even if the condition of the while is still true (because the break when i vale 3, then the condition i < 10 is still true), the loop is all that’s inside do { } while.

What comes after is actually a code block "isolated", that is not part of the do...while. The problem is that in Javascript the semicolon at the end of statements is optional (see more on the subject here, here and here), so this:

do {
    abc
} while (condição) {
    xyz
}

It’s the same as that:

do {
    abc
} while (condição); // ponto-e-vírgula, indica que o do...while terminou

{ // chaves indica o início de um bloco de código
    xyz
}

You can test it yourself on this website (in the "Format" tab it is possible to see where the semicolon is inserted).


Anyway, maybe you don’t even need a pad do...while, because it has better ways to iterate through an array (not to mention that you are comparing i < data, that is, a number with an array, and also not incrementing the i nowhere).

The content of data, but assuming that the items are sorted by date, a way to get the output you want would be:

let currentDate = null;
for (const d of data) {
    let [id, de, assunto, conteudo, prioridade, hora, date] = d;
    if (currentDate != date) {
        linha += `<tr><td>${date}</td></tr>`;
        currentDate = date;
    }
    linha += `<tr>
        <td>${de}</td> 
       <td>${assunto}</td>
       <td>${prioridade}</td> 
       <td>${hora}</td> 
       </tr>`;
}

1

I believe you need to initially reread your own code and look for errors that are obvious, for example:

Uncaught Referenceerror: Data is not defined at Object.Success

This mistake is telling you that Data does not exist, was not declared or initialized in any part of your code, maybe you wanted to use data which is the name of the variable you set here $.getJSON('./alertas', function (data), variables with uppercase letters and minuscules are different.

Clearly a little reading on the construct can also help you do/while, is basically the same as while normal, except for one thing, your code will always run at least 1 time. Example:

let i = 4;
while(i < 3){
  console.log(i++);
}

Basically in the code above, if the variable i has a value greater than or equal to 3, the code will not be executed, this because the control of the condition is done before execution. What happens when you use the do/while is that first it will run 1x the code and only then control the desired condition, example:

let i = 4;
do{
  console.log(i++);
}while(i < 3);

  • thanks for the clarification.

Browser other questions tagged

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