Which method to use to make the code not large

Asked

Viewed 80 times

0

Good morning!!!

I am creating a code that creates all possibilities with integer values filled in the fields, or that are emptiness, I went to test for every field I add in my html, and each field I set doubled the number of conditions to check if the fields are empty or filled. So far I’m using 3 fields to generate the possibilities. I’d like to create some method that doesn’t get the code too big.

function gerar() {
  n1 = document.getElementById("n1").value;
  n2 = document.getElementById("n2").value;
  n3 = document.getElementById("n3").value;
  resultado = "";
  //Gera combinações de números somente com números que falta para completar:

  if (n1 == "" && n2 == "" && n3 == "") {
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        for (k = 0; k < 10; k++) {
          resultado += i + "-" + j + "-" + k + "<br>";
        }
      }
    }
  } else if (n1 == "" && n2 == "" && n3 != "") {
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        resultado += i + "-" + j + "-" + n3 + "<br>";
      }
    }
  } else if (n1 == "" && n2 != "" && n3 == "") {
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        resultado += i + "-" + n2 + "-" + j + "<br>";
      }
    }
  } else if (n1 == "" && n2 != "" && n3 != "") {
    for (i = 0; i < 10; i++) {
      resultado += i + "-" + n2 + "-" + n3 + "<br>";
    }
  } else if (n1 != "" && n2 == "" && n3 == "") {
    for (i = 0; i < 10; i++) {
      for (j = 0; j < 10; j++) {
        resultado += n1 + "-" + i + "-" + j + "<br>";
      }
    }
  } else if (n1 != "" && n2 == "" && n3 != "") {
    for (i = 0; i < 10; i++) {
      resultado += n1 + "-" + i + "-" + n3 + "<br>";
    }
  } else if (n1 != "" && n2 != "" && n3 == "") {
    for (i = 0; i < 10; i++) {
      resultado += n1 + "-" + n2 + "-" + i + "<br>";
    }
  } else {
    resultado += n1 + "-" + n2 + "-" + n3 + "<br>";
  }
  document.getElementById("resultado").innerHTML = resultado;
}
input {
  width: 25px;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
  <title>Gerador de possibilidades números</title>
  <meta charset="UTF-8">
</head>

<body>
  <input type="number" id="n1" value=""> -
  <input type="number" id="n2" value=""> -
  <input type="number" id="n3" value="">
  <button onclick="gerar()">Gerar</button>
  <div id="resultado"></div>
</body>

</html>

  • I’m not sure what you mean... for instance if you insert 1-3-5 want to show 1 result or multiple? can give examples?

  • show only 1, if you leave some field empty, it will generate the possibilities of combinations. do a test with 1, 2 or all fields empty

1 answer

1


I didn’t understand the purpose, but I can play with some checks. In this case, as an empty string converted to integer results in NaN, I checked if the return was not Nan to control the loop.

function gerar() {
  n1 = parseInt(document.getElementById("n1").value);
  n2 = parseInt(document.getElementById("n2").value);
  n3 = parseInt(document.getElementById("n3").value);
  resultado = "";
  
  for (i = n1 || 0, ix = isNaN(n1) ? 10 : i+1; i < ix; i++) {
    for (j = n2 || 0, jx = isNaN(n2) ? 10 : j+1; j < jx; j++) {
      for (k = n3 || 0, kx = isNaN(n3) ? 10 : k+1; k < kx; k++) {
         resultado += i + "-" + j + "-" + k + "<br>";
      }	
    }	
  }
  document.getElementById("resultado").innerHTML = resultado;
}
input {
  width: 25px;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
  <title>Gerador de possibilidades números</title>
  <meta charset="UTF-8">
</head>

<body>
  <input type="number" id="n1" value=""> -
  <input type="number" id="n2" value=""> -
  <input type="number" id="n3" value="">
  <button onclick="gerar()">Gerar</button>
  <div id="resultado"></div>
</body>

</html>

Browser other questions tagged

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