How can I optimize my code?

Asked

Viewed 82 times

2

Tabela

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("");
        }
    }
  • 3

    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).

  • 2

    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.

  • 1

    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?

  • 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

  • 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.

  • I’ll put a picture

  • 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?

  • need to follow this pattern that is not I q am setting, the two values have to appear

  • The image did not clarify in any way the doubt, I recommend reading this: https://answall.com/help/how-to-ask

  • 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?

  • these numbers I used an Alert to do as pd see in the code did not use variables for them

  • jsfiddle.net/qdkha3nd/ maybe this will help

Show 7 more comments

1 answer

2


I will use an approach of creating a JSON table. See the code below. Click the blue button Execute to test.

function formatarElemento(e) {
    return e.length === 2
        ? e[0].a + " / " + e[0].b + " - " + e[1].a + " / " + e[1].b
        : e[0].a + " / " + e[0].b;
}

function par(a, b) {
    return {"a": a, "b": b};
}

function celula1(par) {
    return [par];
}

function celula2(par1, par2) {
    return [par1, par2];
}

var tabela = {
    "-8": {
       "3": celula2(par( 90, 200), par(120, 300)),
       "2": celula2(par( 90, 200), par(120, 300)),
       "1": celula2(par( 90, 200), par(120, 300)),
       "0": celula2(par(120, 200), par(120, 300)),
      "-1": celula2(par(120, 200), par(120, 300)),
      "-2": celula2(par(120, 200), par(120, 300)),
      "-3": celula2(par(160, 200), par(160, 300)),
      "-4": celula2(par(160, 200), par(160, 300)),
      "-5": celula2(par(160, 200), par(160, 300)),
      "-6": celula2(par(160, 200), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 350)),
      "-8": celula2(par( 90, 250), par(160, 350))
    },
    "-7": {
       "3": celula2(par( 90, 200), par(120, 300)),
       "2": celula2(par( 90, 200), par(120, 300)),
       "1": celula2(par( 90, 200), par(120, 300)),
       "0": celula2(par(120, 200), par(120, 300)),
      "-1": celula2(par(120, 200), par(120, 300)),
      "-2": celula2(par(120, 200), par(120, 300)),
      "-3": celula2(par(160, 200), par(160, 300)),
      "-4": celula2(par(160, 200), par(160, 300)),
      "-5": celula2(par(160, 200), par(160, 300)),
      "-6": celula2(par(160, 200), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 350)),
      "-8": celula2(par( 90, 200), par(160, 350))
    },
    "-6": {
       "3": celula2(par( 90, 200), par(120, 300)),
       "2": celula2(par( 90, 200), par(120, 300)),
       "1": celula2(par( 90, 200), par(120, 300)),
       "0": celula2(par(120, 200), par(120, 300)),
      "-1": celula2(par(120, 200), par(120, 300)),
      "-2": celula2(par(120, 200), par(120, 300)),
      "-3": celula2(par(160, 200), par(160, 300)),
      "-4": celula2(par(160, 200), par(160, 300)),
      "-5": celula2(par(160, 200), par(160, 300)),
      "-6": celula2(par(160, 200), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 350)),
      "-8": celula2(par( 90, 150), par(160, 350))
    },
    "-5": {
       "3": celula2(par( 90, 200), par( 90, 250)),
       "2": celula2(par( 90, 200), par( 90, 250)),
       "1": celula2(par( 90, 200), par( 90, 250)),
       "0": celula2(par(120, 200), par(120, 250)),
      "-1": celula2(par(120, 200), par(120, 250)),
      "-2": celula2(par(120, 200), par(120, 250)),
      "-3": celula2(par(160, 150), par(160, 250)),
      "-4": celula2(par(160, 200), par(160, 300)),
      "-5": celula2(par(160, 200), par(160, 300)),
      "-6": celula2(par(160, 200), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 350)),
      "-8": celula2(par(120, 200), par(160, 350))
    },
    "-4": {
       "3": celula2(par( 90, 150), par( 90, 200)),
       "2": celula2(par( 90, 150), par( 90, 200)),
       "1": celula2(par( 90, 150), par( 90, 200)),
       "0": celula2(par(120, 150), par(120, 200)),
      "-1": celula2(par(120, 150), par(120, 200)),
      "-2": celula2(par(120, 200), par(120, 250)),
      "-3": celula2(par(160, 150), par(160, 250)),
      "-4": celula2(par(160, 150), par(160, 250)),
      "-5": celula2(par(160, 150), par(160, 250)),
      "-6": celula2(par(160, 200), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 350)),
      "-8": celula2(par(120, 250), par(160, 350))
    },
    "-3": {
       "3": celula2(par( 90, 150), par( 90, 200)),
       "2": celula2(par( 90, 150), par( 90, 200)),
       "1": celula2(par( 90, 150), par( 90, 200)),
       "0": celula2(par(120, 150), par(120, 200)),
      "-1": celula2(par(120, 150), par(120, 200)),
      "-2": celula2(par(120, 150), par(120, 200)),
      "-3": celula2(par(160, 150), par(160, 200)),
      "-4": celula2(par(160, 150), par(160, 250)),
      "-5": celula2(par(160, 150), par(160, 250)),
      "-6": celula2(par(160, 200), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 350)),
      "-8": celula1(               par(210, 300))
    },
    "-2": {
       "3": celula1(               par(160, 150)),
       "2": celula1(               par(160, 150)),
       "1": celula1(               par(160, 150)),
       "0": celula1(               par(160, 150)),
      "-1": celula1(               par(160, 150)),
      "-2": celula1(               par(160, 200)),
      "-3": celula2(par(160, 150), par(160, 200)),
      "-4": celula2(par(160, 150), par(160, 200)),
      "-5": celula2(par(160, 200), par(160, 200)),
      "-6": celula2(par(160, 250), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 300)),
      "-8": celula2(par(160, 250), par(150, 350))
    },
    "-1": {
       "3": celula1(               par(160, 150)),
       "2": celula1(               par(160, 150)),
       "1": celula1(               par(160, 150)),
       "0": celula1(               par(160, 150)),
      "-1": celula1(               par(160, 150)),
      "-2": celula1(               par(160, 200)),
      "-3": celula2(par(160, 150), par(160, 200)),
      "-4": celula2(par(160, 150), par(160, 200)),
      "-5": celula2(par(160, 200), par(160, 250)),
      "-6": celula2(par(160, 250), par(160, 300)),
      "-7": celula2(par(160, 250), par(160, 300)),
      "-8": celula2(par(160, 300), par(160, 350))
    }
};

function calcular() {
    var abscissa = document.getElementById('ab').value;
    var ordenada = document.getElementById('or').value;
    if (abscissa < -8) abscissa = -8;
    if (ordenada < -8) ordenada = -8;
    var elemento = tabela["" + ordenada]["" + abscissa];
    document.getElementById('res').value = formatarElemento(elemento);
}
<p>
    Abscissa (coluna da tabela): <input id="ab" type="text" />
</p>
<p>
    Ordenada (linha da tabela): <input id="or" type="text" />
</p>
<p>
    <input type="button" value="Calcular" onclick="javascript: calcular();" />
</p>
<p>
    Resultado: <input id="res" type="text" readonly />
</p>

I called the elements of each pair of a and b, because I didn’t know how to name them. To change this, just change the names in the functions par and formatarElemento.

  • It was good to enjoy a lot but something is wrong -8 -1 should give 160/300 160/350 not 160/250 160/350. Edit1: and only I change vlw I think this better than that bit of if

  • @Lucassimao had just copied that wrong number. I’ve already edited the answer. Still have to do the boring job of checking those numbers all one by one.

  • ok old brigade got mt smaller and easier to understand.

  • As they would say, here have courage and give the fish do not teach to fish :P ... just kidding, but honestly you practically did

  • @Guilhermenascimento I taught him another way of doing that was not a lot of ifs. Now he will take this knowledge with him.

Browser other questions tagged

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