Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
I want my javascript to move to the next if statement if the first one evaluates as TRUE

I want to check for negative values and if there is change them to its default value.

for (i = 0; i <= 1; i++) {
    //I want this to run this statement once and if its true perform all the functions and move to the next one
    if (h1 < 0 || h2 < 0) {
        alert("negative number is not acceptable here");
        document.getElementById("up1").value = document.getElementById("up1").defaultValue;
        document.getElementById("q1").value = document.getElementById("q1").defaultValue;
        document.getElementById("tot1").value = document.getElementById("tot1").defaultValue;
        Total1 = 0.00;
        continue;

    }
    else if (h3 < 0 || h4 < 0) {
        alert("No Negative here as well")
        document.getElementById("up2").value = document.getElementById("up2").defaultValue;
        document.getElementById("q2").value = document.getElementById("q2").defaultValue;
        document.getElementById("tot2").value = document.getElementById("tot2").defaultValue;
        continue;

    }
    //I have a let Total1 & Total2 variables at the beggining of the code set to zero
    //
    Total2 = h3 * h4;
}    

Hi guys is there anyway to continue to the next else if statement if the first IF statement evaluates to TRUE and runs all the functions in it. Currently, if the first IF statement evaluates to true it performs all the functions in it and ends the loop. I want it to check each variable for a negative without repeating the code all over again.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There are two cases for your problem.

  1. You want to run the second block only if the first if block is true.
  2. You want to run the second block either the first block is true or false

First case

isFirstBlockTrue = false
if (h1 < 0 || h2 < 0) {
   isFirstBlockTrue = true
   /* rest of your code */
}

if(isFirstBlockTrue){

  if (h3 < 0 || h4 < 0) {
    /* rest of your code */
  }

}

 /* rest of your code */

Second Case

if (h1 < 0 || h2 < 0) {
  /* first block code */
}
if (h3 < 0 || h4 < 0) {
  /* second block code */
}

Note: Don't use the continue otherwise it will skip the iteration

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to run the second block only if the first if block is true:

if (h1 < 0 || h2 < 0) {
   /* Your code */

   if(h3 < 0 || h4 < 0){
       /* rest of your code */
   }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You just need two linear if statements.

for (i = 0; i <= 1; i++) {
    //I want this to run this statement once and if its true perform all the functions and move to the next one

    if (h1 < 0 || h2 < 0) {
        alert("negative number is not acceptable here");
        document.getElementById("up1").value = document.getElementById("up1").defaultValue;
        document.getElementById("q1").value = document.getElementById("q1").defaultValue;
        document.getElementById("tot1").value = document.getElementById("tot1").defaultValue;
        Total1 = 0.00;
    }
    

   if (h3 < 0 || h4 < 0) {
       alert("No Negative here as well")
       document.getElementById("up2").value = document.getElementById("up2").defaultValue;
       document.getElementById("q2").value = document.getElementById("q2").defaultValue;
       document.getElementById("tot2").value = document.getElementById("tot2").defaultValue; 
     }

    Total2 = h3 * h4;
}  
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda