Q: How can I do this in a loop jquery? I'm trying to make a game. I need to increase the number of iteracctions each time I execute it. The problem is that if I use a loop the jquery animates works all at once. I need them to execute individually one after other.
Here I leve the code. Sorry if it too long but I think it make the problem clear.
<!DOCTYPE html>
<html>

<head>
<title>Examen</title>
<meta charset="utf-8">
<script src="jquery-3.4.1.min.js"></script>
<style>
    td{
        width: 100px;
        height: 100px;
        border: 2px solid black;
        text-align: center;
        vertical-align: middle;
    }

    #td1 {
        background-color: red;
    }

    #td2 {
        background-color: green;
    }

    #td3 {
        background-color: yellow;
    }

    #td4 {
        background-color: purple;
    }

    #td5 {
        background-color: black;
        color: white;
    }

    #td6 {
        background-color: orangered;
    }

    #td7 {
        background-color: blue;
    }

    #td8 {
        background-color: pink;
    }

    #td9 {
        background-color: brown;
    }
</style>
</head>

<body>
<table>
    <tr>
        <td id="td1">1</td>
        <td id="td2">2</td>
        <td id="td3">3</td>
    </tr>
    <tr>
        <td id="td4">4</td>
        <td id="td5">5</td>
        <td id="td6">6</td>
    </tr>
    <tr>
        <td id="td7">7</td>
        <td id="td8">8</td>
        <td id="td9">9</td>
    </tr>
</table>
<br>
<script>
    var num = new Array(5);
    for (var i = 0; i < 5; i++) {
        num[i] = Math.ceil(Math.random() * 9);
    }

    /* for(var j=0; j<5; j++){
         $("#td"+num[j]).animate({ opacity : 0.0},1500,function(){
             $("#td"+num[j]).animate({ opacity : 1.0},1500);
         });
     }*/

    $("#td" + num[0]).animate({ opacity: 0.0 }, 1000, function () {
        $("#td" + num[0]).animate({ opacity: 1.0 }, 1000, function () {
            $("#td" + num[1]).animate({ opacity: 0.0 }, 1000, function () {
                $("#td" + num[1]).animate({ opacity: 1.0 }, 1000, function () {
                    $("#td" + num[2]).animate({ opacity: 0.0 }, 1000, function () {
                        $("#td" + num[2]).animate({ opacity: 1.0 }, 1000, function ()     {
                            $("#td" + num[3]).animate({ opacity: 0.0 }, 1000, function () {
                                $("#td" + num[3]).animate({ opacity: 1.0 }, 1000, function () {
                                    $("#td" + num[4]).animate({ opacity: 0.0 }, 1000, function () {
                                        $("#td" + num[4]).animate({ opacity: 1.0 }, 1000);
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });

    var colores = [];
    $("td").click(function () {
        var num2;


        switch ($(this).prop("id")) {
            case "td1": num2 = 1; break;
            case "td2": num2 = 2; break;
            case "td3": num2 = 3; break;
            case "td4": num2 = 4; break;
            case "td5": num2 = 5; break;
            case "td6": num2 = 6; break;
            case "td7": num2 = 7; break;
            case "td8": num2 = 8; break;
            case "td9": num2 = 9; break;
        }
        colores.push(num2);

        if (colores.length == 5) {
            var correcto = true;
            for (var i = 0; i < 5 && correcto == true; i++) {
              //  alert(colores[i]+"-"+num[i]);
                if (colores[i] != num[i]) {
                    correcto = false;
                }
            }
            if (correcto) {
                    alert("Acertastes");
                } else {
                    alert("Fallastes");
                }
        }

    });
</script>
</body>
</html>


A: Would a recursive function be enough instead of a loop. Im guessing you need the loop because you want it to be dynamic


function animateMe(animateElementsIds, idIndex = 0){
    $("#td" + animateElementsIds[idIndex]).animate({ opacity: 0.0 }, 1000, function () {
        $("#td" + animateElementsIds[idIndex]).animate({ opacity: 1.0 }, 1000, function () {
          animateMe(animateElementsIds, ++idIndex);
        });
    });
}

var num = new Array(5);
    for (var i = 0; i < 5; i++) {
        num[i] = Math.ceil(Math.random() * 9);
    }

    /* for(var j=0; j<5; j++){
         $("#td"+num[j]).animate({ opacity : 0.0},1500,function(){
             $("#td"+num[j]).animate({ opacity : 1.0},1500);
         });
     }*/

    animateMe(num);

    var colores = [];
    $("td").click(function () {
        var num2;


        switch ($(this).prop("id")) {
            case "td1": num2 = 1; break;
            case "td2": num2 = 2; break;
            case "td3": num2 = 3; break;
            case "td4": num2 = 4; break;
            case "td5": num2 = 5; break;
            case "td6": num2 = 6; break;
            case "td7": num2 = 7; break;
            case "td8": num2 = 8; break;
            case "td9": num2 = 9; break;
        }
        colores.push(num2);

        if (colores.length == 5) {
            var correcto = true;
            for (var i = 0; i < 5 && correcto == true; i++) {
              //  alert(colores[i]+"-"+num[i]);
                if (colores[i] != num[i]) {
                    correcto = false;
                }
            }
            if (correcto) {
                    alert("Acertastes");
                } else {
                    alert("Fallastes");
                }
        }

    });
td{
        width: 100px;
        height: 100px;
        border: 2px solid black;
        text-align: center;
        vertical-align: middle;
    }

    #td1 {
        background-color: red;
    }

    #td2 {
        background-color: green;
    }

    #td3 {
        background-color: yellow;
    }

    #td4 {
        background-color: purple;
    }

    #td5 {
        background-color: black;
        color: white;
    }

    #td6 {
        background-color: orangered;
    }

    #td7 {
        background-color: blue;
    }

    #td8 {
        background-color: pink;
    }

    #td9 {
        background-color: brown;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
    <tr>
        <td id="td1">1</td>
        <td id="td2">2</td>
        <td id="td3">3</td>
    </tr>
    <tr>
        <td id="td4">4</td>
        <td id="td5">5</td>
        <td id="td6">6</td>
    </tr>
    <tr>
        <td id="td7">7</td>
        <td id="td8">8</td>
        <td id="td9">9</td>
    </tr>
</table>


