Query Exercise Arrays and Dictionaries JS

0

I am doing an introductory course in programming, we use JavaScript and the browser console for it, we saw types of data, operations, variables and their comparison, structures of selection and iteration, arrays, the theme is that I I have been stuck when the topic of the dictionaries and the use together with the arrays began to add up, it is a topic that I do not understand anything, they gave me a practical exercise for which I have access to the solution, but I do not want to continue forward without having understood the solution

  

The exercise is:

     
  • Program an example that stores in an array all the cards in the deck, each one represented with a dictionary that has a suit and   a value.
  •   
  • Starting from the array you have built in the previous exercise, build an algorithm that filters in a new array only the letters   red, and in another array the black letters.
  •   
  • Starting from the array you have built in the previous exercise, build an algorithm that filters the red and red letters in a new array   couple Then print the last letter of that new array in the console.
  •   

    And the solution is:

    var baraja = [];
    var palos = ["c", "p", "t", "d"];
    for (var i = 0; i < palos.length; i = i + 1) {
     for (var j = 1; j <= 12; j = j + 1) {
     baraja[baraja.length] = { p: palos[ i ], v: j };
     }
    }
    var rojas = [];
    var negras = [];
    for (var i = 0; i < baraja.length; i = i + 1) {
     if (baraja[ i ].palo === "c" || baraja[ i ].palo === "d") {
     rojas[rojas.length] = baraja[ i ];
     } else {
     if (baraja[ i ].palo === "p" || baraja[ i ].palo === "t") {
     negras[negras.length] = baraja[ i ];
     }
     }
    }
    var rojaspares = [];
    for (var i = 0; i < rojas.length; i = i + 1) {
     if (rojas[ i ].valor % 2 === 0) {
     rojaspares[rojaspares.length] = rojas[ i ];
     }
    }
    var ultimacarta = { palo: rojaspares[rojaspares.length - 1].palo,
     valor: rojaspares[rojaspares.length - 1].valor };
    console.log(ultimacarta);
    

    If someone can develop and explain the solution, or tell me their explanation, logic or reasoning, I would like to understand it and be able to move forward, thanks

        
    asked by Fernando 01.08.2018 в 15:04
    source

    1 answer

    1

    First you declare a variable named shuffle and assign it the value of a empty array .

    var baraja = [];
    

    Then a variable named sticks and assigned an arrangement with 4 elements ("hearts", "sticks", "clover", "diamonds").

    var palos = ["c", "p", "t", "d"];
    

    The following is a for which consists of the following:

    a variable with name i with an initial value of 0 , followed by a parameter where we indicate when the iteration will stop (palos.length) and finally increase in one the value of i for each time you go through the for .

    Within the first for , there is another iteration, now with the incremental variable j and initial value 1 , we define a static stop 12 cycles (Number of cards per form) and increase by one again.

    for (var i = 0; i < palos.length; i = i + 1) {
     for (var j = 1; j <= 12; j = j + 1) {
     baraja[baraja.length] = { p: palos[ i ], v: j };
     }
    }
    

    In total they are 48 cycles and we assign that value to deck .

    Now we declare two new variables (red and black) assigning them fixes empty.

    var rojas = [];
    var negras = [];
    

    Again a for with the variable i in 0 , with the limit of 48 of Shuffle obtained from the previous exercise. Within this iteration we include a conditional with if where we will separate red and black:

    Yes the letter is equal to c baraja[ i ].palo === "c" or if the letter is equal to d baraja[ i ].palo === "d" we will assign them to red

      

    Remember in a if this: || is equivalent to a OR

    On the other hand, if the card contains p or t , the cards will be assigned to blacks.

    for (var i = 0; i < baraja.length; i = i + 1) {
     if (baraja[ i ].palo === "c" || baraja[ i ].palo === "d") {
     rojas[rojas.length] = baraja[ i ];
     } else {
     if (baraja[ i ].palo === "p" || baraja[ i ].palo === "t") {
     negras[negras.length] = baraja[ i ];
     }
     }
    }
    

    We declare a variable with the name rojopares and assign it an empty array.

    var rojaspares = [];
    

    A final iteration with a condition to identify the pairs of red cards.

    for (var i = 0; i < rojas.length; i = i + 1) {
     if (rojas[ i ].valor % 2 === 0) {
     rojaspares[rojaspares.length] = rojas[ i ];
     }
    }
    

    Declares a last variable ultimacarta by assigning an object with two values (type and number).

    var ultimacarta = { 
        palo: rojaspares[rojaspares.length - 1].palo,
        valor: rojaspares[rojaspares.length - 1].valor 
     };
    

    We show the result in console.

    console.log(ultimacarta);
    
        
    answered by 01.08.2018 / 15:46
    source