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