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?– Sergio
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
– Anderson Marchi