0
I’m building an app on the bird site. Inside the site will have an article informing the possible genetics that will be generated through a mating of two different breeds.
Example: Ring Neck - Albino x Ring Neck - Blue can generate Ring Neck - Violet.
For this I created in HTML a select listing all males, and depending on the chosen male will be generated another list (another select below), with all females that pair with it.
After selected the male and the female, when you click on button check, it will bring within a textarea the result.
PROBLEM
As there are several races, there are several validations, I would like your help if I can reduce the amount of if, Else if.
var box01 = document.getElementById("box01");
var box02 = document.getElementById("box02");
var txtArea = document.getElementById("txtArea");
function myChange(){
box02.innerHTML = "";
box02.removeAttribute("disabled");
if(box01.value == "nenhum"){
box02.setAttribute("disabled");
}
if(box01.value == "albino"){
var optionArray = ["0|Nenhum","albino|Albino","azul|Azul",
"canela|Canela","cremino|Cremino","turquesa-azul|Turquesa Azul","verde-azul|Verde/Azul","lutino|Lutino","lutino-azul|Lutino/Azul","turquesa-cinza|Turquesa Cinza","skyblue|SkyBlue"];
}
else if(box01.value == "azul"){
var optionArray = ["0|Nenhum","azul|Azul","lutino|Lutino","lutino-azul|Lutino/Azul","albino|Albino","cinza|Cinza","verde-azul|Verde/Azul","verde-cinza-azul|Verde Cinza/Azul","turquesa-azul|Turquesa Azul","silver|Silver","skyblue|SkyBlue","turquesa-cinza|Turquesa Cinza","cremino|Cremino"];
}
else if(box01.value == "canela"){
var optionArray = ["0|Nenhum","cinza|Cinza"];
}
else if(box01.value == "canela-azul"){
var optionArray = ["0|Nenhum","skyblue|SkyBlue","cinza|Cinza","azul|Azul","canela-verde-cinza-azul|Canela Verde Cinza/Azul","silver|Silver"];
}
for(var option in optionArray){
// Método .sort() ordena o elementos do próprio array.
// Método foi utilizado para organizar a exibição da box02(fêmeas) em ordem alfabetica.
optionArray.sort();
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
box02.options.add(newOption);
}
}
function myFunction(){
// Validação Albino
if(box01.value == "albino" && box02.value == "0"){
txtArea.innerHTML = "";
}
else if(box01.value == "albino" && box02.value == "verde-azul"){
txtArea.innerHTML = "M Verde/Azul\nM Azul\nF Lutino/Azul\nF Albino";
}
else if(box01.value == "albino" && box02.value == "albino"){
txtArea.innerHTML = "M Albino\nF Albino";
}
else if(box01.value == "albino" && box02.value == "azul"){
txtArea.innerHTML = "M Azul\nF Albino";
}
else if(box01.value == "albino" && box02.value == "cremino"){
txtArea.innerHTML = "M Cremino\nM Albino\n\nF Cremino\nF Albino";
}
else if(box01.value == "albino" && box02.value == "turquesa-azul"){
txtArea.innerHTML = "M Turquesa/Azul\nM Azul\n\nF Cremino\nF Albino";
}
else if(box01.value == "albino" && box02.value == "lutino"){
txtArea.innerHTML = "M Lutino/Azul\nF Lutino/Azul";
}
else if(box01.value == "albino" && box02.value == "lutino-azul"){
txtArea.innerHTML = "M Lutino/Azul\nM Albino\n\nF Lutino/Azul\nF Albino";
}
}
<form>
<div id="container-form">
<label for="box01">Macho</label>
<select id="box01" name="box01" onchange="myChange()">
<option value="nenhum">Nenhum</option>
<option value="albino">Ring Neck - Albino</option>
<option value="azul">Ring Neck - Azul</option>
<option value="canela">Ring Neck - Canela</option>
<option value="canela-azul">Ring Neck - Canela Azul</option>
<option value="canela-verde-cinza">Ring Neck - Canela Verde Cinza</option>
<option value="cinza">Ring Neck - Cinza</option>
<option value="cremino">Ring Neck - Cremino</option>
<option value="lutino">Ring Neck - Lutino</option>
<option value="silver">Ring Neck - Silver</option>
<option value="skyblue">Ring Neck - SkyBlue</option>
<option value="turquesa-azul">Ring Neck - Turquesa Azul</option>
<option value="turquesa-cinza">Ring Neck - Turquesa Cinza</option>
<option value="turquesa-skyblue">Ring Neck - Turquesa SkyBlue</option>
<option value="turquesa-silver">Ring Neck - Turquesa Silver</option>
<option value="verde-cinza">Ring Neck - Verde Cinza</option>
<option value="verde">Ring Neck - Verde</option>
<option value="pallids">Ring Neck - Pallids</option>
</select>
<label for="box01">Fêmea</label>
<select id="box02" disabled>
</select>
<button type="button" onclick="myFunction()">Verificar</button>
</div>
<textarea id="txtArea" rows="10" cols="29" disabled></textarea>
</form>
The Final would be another validation. Example let’s assume that in the first field "select male" chose albino and in the second "select female" chose blue .... ai within the textarea would have to give the result of crossing the genetics of the two. In this case I would have to create another object?
– Gabriel Mariano
@Gabrielmariano So it would be this object
cruzamentos
that I said at the end– hkotsubo
ah I got it, thank you very much! I’m going to take a test.
– Gabriel Mariano
@Gabrielmariano Blz. Don’t forget that, if the answer solve your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. And when I get 15 points, you can also vote in all the answers you found useful.
– hkotsubo
Leave it ;] ! I’m running the tests.
– Gabriel Mariano
I’m sorry for my ignorance @hkotsubo, but I couldn’t understand the Cross checkCross( ) ...being more specific, I couldn’t understand how it’s validating the " crossings[box1.value][box02.value] "
– Gabriel Mariano
@Gabrielmariano It checks if there is value in the object
cruzamentos
. For example, if you choose "none", the value of the boxes will be0
and to fetchcruzamentos[0]
You won’t find anything (and then he falls in Else) - I edited now to change the check, because both the male and the female can be "none", so theif
have to treat these 2 cases– hkotsubo
@Gabrielmariano Complementando, this
if
works because null values are consideredfalse
, so you can test any object on an if, see more about it here: https://answall.com/q/271693/112052– hkotsubo
It worked, thank you!
– Gabriel Mariano