Javascript: Is there a way to do a do-while test the condition before the end of the loop?

Asked

Viewed 43 times

-1

Context: I’m developing a text-based RPG and at the time of doing the battle mechanics I’m having this 'problem'.

Ex:

    function verificarEstado(pessoa){
     if (pessoa.estado != 'normal') {
      /* aqui terá um switch para cada efeito tipo envenenado, sangrando, etc, esses efeitos podem causar dano, então pode ser que um dos combatentes morra nessa etapa*/
      verificarVida(pessoa.vida);
    }
    
    function verificarVida(pessoa){
      if (pessoa.vida<=0){
        /* aqui vem a condição para parar o laço, pois se um dos combatentes morreu, é o fim da luta */
      }
    }
    
    function realizarAtaque(atacante, alvo){
     /* Não vou entrar em detalhes aqui porque não é relevante, o ponto é que eu não quero que essa etapa seja executada se um dos personagens tiver morrido no 'verificarEstado()  */
    }
    
    function iniciarBatalha(jogador, inimigo){
      let ambosVivos=true;
      let turno=1;
    
       do{
    
        verificarEstado(jogador);
        verificarEstado(inimigo);
       
        realizarAtaque(jogador, inimigo);
        realizarAtaque(inimigo, jogador);
       
       turno++;
       } while (ambosVivos);
      }
iniciarBatalha(jogador,inimigo) /* Chamada para o inicio da batalha    */
    
  • 1

    What problem are you having? It wasn’t clear

  • I didn’t understand the doubt?

  • Inside Do-while I’m performing 4 functions, right? I want to know if there is a way to, for example, the first of these functions already change the check condition and the others not be executed. In this context, a character may die in the first of these four functions, so it would make no sense for him to attempt an attack on the next function. I could put an if(ambiguous) involving the functions realizarAtaque(), but it doesn’t seem to be the best way. I’m looking for something like the "keep on" loop for.

1 answer

-1


Javascript: There is a way to do a do-while test the condition before the end of the loop?

Directly answering the question’s title

In any repeating structure in JS, you can perform a break to stop the loop, or continue to start the next iteration and stop the current one. The continue in this case will only go to the next iteration if the loop condition remains true.

function loop() {
  let i = 0
  do {
    if(i ==3) {
      i += 2
      alert("Continue irá pular para a próxima iteração")
      continue
    }
    
    if(i == 5) {      
      alert("Parar o loop")
      break
    }
    alert("iteração normal")
    i++
  } while(i < 6)
}

loop()

a character may die in the first of these four functions, so no it would make sense for him to attempt an attack on the next function.

Being very simple in the solution according to what you showed us, the functions that can stop the loop should return a boolean indicating whether or not the loop should continue. If you should not continue, you can perform a break.

Browser other questions tagged

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