Lacos while em javascript

Asked

Viewed 40 times

1

I need to create a while loop that sent a message x number of times to the console.

var loop = function(){
    while(){
        console.log("");
    }
}

loop(); 
  • 1

    If you already have the number x of times why not use one() ?

  • Yes, using the for would be easier, but I would like to learn how it would work to make the same question that would be asked with the for but with the while.

1 answer

3

Just create a condition:

var loop = function(x) {
  qtd = 0;
  while (qtd < x) {
    console.log("Mensagem #" + qtd);
    qtd++;
  }
}

loop(10);

Browser other questions tagged

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