2
I would like to optimize my code but I don’t know how to do it. It has to produce a result just like the one in the table above.
Code:
function calcular() {
abiscissa = document.getElementById('ab').value;
ordenada = document.getElementById('or').value;
// Ordenada -1
if (abiscissa == +3 && ordenada == -1) {
alert("160/150");
} else if (abiscissa == +2 && ordenada == -1) {
alert("160/150");
} else if (abiscissa == +1 && ordenada == -1) {
alert("160/150");
} else if (abiscissa == 0 && ordenada == -1) {
alert("160/150");
} else if (abiscissa == -1 && ordenada == -1) {
alert("160/150");
} else if (abiscissa == -2 && ordenada == -1) {
alert("210/150");
} else if (abiscissa == -3 && ordenada == -1) {
alert("210/200");
} else if (abiscissa == -4 && ordenada == -1) {
alert("160/150 \n160/200");
} else if (abiscissa == -5 && ordenada == -1) {
alert("160/200 \n160/250");
} else if (abiscissa == -6 && ordenada == -1) {
alert("160/250 \n160/300");
} else if (abiscissa == -7 && ordenada == -1) {
alert("160/250 \n160/300");
} else if (abiscissa == -8 && ordenada == -1) {
alert("160/300 \n160/350");
}
// Ordenada -2
else if (abiscissa == -8 && ordenada == -2) {
alert("160/150");
} else if (abiscissa == +2 && ordenada == -2) {
alert("160/150");
} else if (abiscissa == +1 && ordenada == -2) {
alert("160/150");
} else if (abiscissa == 0 && ordenada == -2) {
alert("160/150");
} else if (abiscissa == -1 && ordenada == -2) {
alert("160/150");
} else if (abiscissa == -2 && ordenada == -2) {
alert("210/150");
} else if (abiscissa == -3 && ordenada == -2) {
alert("210/200");
} else if (abiscissa == -4 && ordenada == -2) {
alert("210/200");
} else if (abiscissa == -5 && ordenada == -2) {
alert("160/150 \n160/250");
} else if (abiscissa == -6 && ordenada == -2) {
alert("160/200 \n160/300");
} else if (abiscissa == -7 && ordenada == -2) {
alert("160/200 \n160/300");
} else if (abiscissa == -8 && ordenada == -2) {
alert("160/250 \n150/350");
}
// Ordenada -3
else if (abiscissa == -8 && ordenada == -3) {
alert("160/200");
} else if (abiscissa == +2 && ordenada == -3) {
alert("160/200");
} else if (abiscissa == +1 && ordenada == -3) {
alert("160/200");
} else if (abiscissa == 0 && ordenada == -3) {
alert("160/200");
} else if (abiscissa == -1 && ordenada == -3) {
alert("160/200");
} else if (abiscissa == -2 && ordenada == -3) {
alert("210/200");
} else if (abiscissa == -3 && ordenada == -3) {
alert("210/200");
} else if (abiscissa == -4 && ordenada == -3) {
alert("210/250");
} else if (abiscissa == -5 && ordenada == -3) {
alert("210/250");
} else if (abiscissa == -6 && ordenada == -3) {
alert("210/300");
} else if (abiscissa == -7 && ordenada == -3) {
alert("210/300");
} else if (abiscissa == -8 && ordenada == -3) {
alert("210/300");
}
// Ordenada -4
else if (abiscissa == -8 && ordenada == -4) {
alert("160/250");
} else if (abiscissa == +2 && ordenada == -4) {
alert("160/250");
} else if (abiscissa == +1 && ordenada == -4) {
alert("160/250");
} else if (abiscissa == 0 && ordenada == -4) {
alert("160/250");
} else if (abiscissa == -1 && ordenada == -4) {
alert("160/250");
} else if (abiscissa == -2 && ordenada == -4) {
alert("160/250");
} else if (abiscissa == -3 && ordenada == -4) {
alert("160/300");
} else if (abiscissa == -4 && ordenada == -4) {
alert("120/150 \n160/300");
} else if (abiscissa == -5 && ordenada == -4) {
alert("120/150 \n160/300");
} else if (abiscissa == -6 && ordenada == -4) {
alert("120/150 \n160/300");
} else if (abiscissa == -7 && ordenada == -4) {
alert("120/250 \n160/350");
} else if (abiscissa == -8 && ordenada == -4) {
alert("120/250 \n160/350");
}
// Ordenada -5
else if (abiscissa == -8 && ordenada == -5) {
alert("");
} else if (abiscissa == +2 && ordenada == -5) {
alert("");
} else if (abiscissa == +1 && ordenada == -5) {
alert("");
} else if (abiscissa == 0 && ordenada == -5) {
alert("");
} else if (abiscissa == -1 && ordenada == -5) {
alert("");
} else if (abiscissa == -2 && ordenada == -5) {
alert("");
} else if (abiscissa == -3 && ordenada == -5) {
alert("");
} else if (abiscissa == -4 && ordenada == -5) {
alert("");
} else if (abiscissa == -5 && ordenada == -5) {
alert("");
} else if (abiscissa == -6 && ordenada == -5) {
alert("");
} else if (abiscissa == -7 && ordenada == -5) {
alert("");
} else if (abiscissa == -8 && ordenada == -5) {
alert("");
}
// Ordenada -6
else if (abiscissa == -8 && ordenada == -6) {
alert("");
} else if (abiscissa == +2 && ordenada == -6) {
alert("");
} else if (abiscissa == +1 && ordenada == -6) {
alert("");
} else if (abiscissa == 0 && ordenada == -6) {
alert("");
} else if (abiscissa == -1 && ordenada == -6) {
alert("");
} else if (abiscissa == -2 && ordenada == -6) {
alert("");
} else if (abiscissa == -3 && ordenada == -6) {
alert("");
} else if (abiscissa == -4 && ordenada == -6) {
alert("");
} else if (abiscissa == -5 && ordenada == -6) {
alert("");
} else if (abiscissa == -6 && ordenada == -6) {
alert("");
} else if (abiscissa == -7 && ordenada == -6) {
alert("");
} else if (abiscissa == -8 && ordenada == -6) {
alert("");
}
// Ordenada -7
else if (abiscissa == -8 && ordenada == -7) {
alert("");
} else if (abiscissa == +2 && ordenada == -7) {
alert("");
} else if (abiscissa == +1 && ordenada == -7) {
alert("");
} else if (abiscissa == 0 && ordenada == -7) {
alert("");
} else if (abiscissa == -1 && ordenada == -7) {
alert("");
} else if (abiscissa == -2 && ordenada == -7) {
alert("");
} else if (abiscissa == -3 && ordenada == -7) {
alert("");
} else if (abiscissa == -4 && ordenada == -7) {
alert("");
} else if (abiscissa == -5 && ordenada == -7) {
alert("");
} else if (abiscissa == -6 && ordenada == -7) {
alert("");
} else if (abiscissa == -7 && ordenada == -7) {
alert("");
} else if (abiscissa == -8 && ordenada == -7) {
alert("");
}
// Ordenada -8
else if (abiscissa == -8 && ordenada == -8) {
alert("");
} else if (abiscissa == +2 && ordenada == -8) {
alert("");
} else if (abiscissa == +1 && ordenada == -8) {
alert("");
} else if (abiscissa == 0 && ordenada == -8) {
alert("");
} else if (abiscissa == -1 && ordenada == -8) {
alert("");
} else if (abiscissa == -2 && ordenada == -8) {
alert("");
} else if (abiscissa == -3 && ordenada == -8) {
alert("");
} else if (abiscissa == -4 && ordenada == -8) {
alert("");
} else if (abiscissa == -5 && ordenada == -8) {
alert("");
} else if (abiscissa == -6 && ordenada == -8) {
alert("");
} else if (abiscissa == -7 && ordenada == -8) {
alert("");
} else if (abiscissa == -8 && ordenada == -8) {
alert("");
}
}
If you can inform the purpose of the code would give to suggest paths (out examples of reduction of ifs, after all there is more of a way to optimize and more of a meaning for this word).
– Guilherme Nascimento
Is the code the same? It seems inconsistent. Trying to do something on top of something wrong won’t do, you need to make sure this is what you really want.
– Maniero
I am trying to write an answer, but I see that without knowing exactly what you are doing, I run the risk of suggesting some optimization that you will have to undo later, because your code is incomplete. So you can tell what these numbers are that you show us?
– Victor Stafusa
the code of that missing only numbers within the if alerts and type a table a column and a row type line -3 col -2 and a row number -3 col -1 and another number and so on
– Lucas Simao
What is the logic/rule that defines +3/-1 as "160/150" instead of being "210/200" or "120/250 n160/350" or "orange n strawberry"? What makes the values these? That’s what we need to know to optimize your code.
– Victor Stafusa
I’ll put a picture
– Lucas Simao
Why do some places have two values? When only one of them is present, it makes any difference whether it is only the first or only the second?
– Victor Stafusa
need to follow this pattern that is not I q am setting, the two values have to appear
– Lucas Simao
The image did not clarify in any way the doubt, I recommend reading this: https://answall.com/help/how-to-ask
– Guilherme Nascimento
Okay, each cell is found by looking at the abscissa and the ordered and can have up to four numbers inside it. What name do I give these four numbers that are inside each cell?
– Victor Stafusa
these numbers I used an Alert to do as pd see in the code did not use variables for them
– Lucas Simao
jsfiddle.net/qdkha3nd/ maybe this will help
– Lucas Simao