Help with exercise in JS

1

Good afternoon I hope someone can help me, I had to do an exercise in which I stored the cards of a deck, I separated them in black letters and red cards, then I took out the red and even cards and I printed the last card, all of According to the little knowledge I have, I am a project of arepndiz and using the browser console. With what I could I did it this way:

var cartas = ["1c","2c","3c","4c","5c","6c","7c","8c","9c","10c","11c","12c","1d","2d","3d","4d","5d","6d","7d","8d","9d","10d","11d","12d","1t","2t","3t","4t","5t","6t","7t","8t","9t","10t","11t","12t","1p","2p","3p","4p","5p","6p","7p","8p","9p","10p","11p","12p"]
undefined
cartasrojas = []
[]
cartasnegras = []
[]
for (var i = 0; i < cartas.length; i ++) {
    if (cartas[i][1] === "c" || cartas[i][1] === "d" || cartas[i][2] === "c" || cartas[i][2] === "d"){
        cartasrojas[cartasrojas.length] = cartas[i];
    } else {
        cartasnegras[cartasnegras.length] = cartas[i];
   }
}
"12p"
cartasrojasypares = []
[]
for (var i = 0; i < cartasrojas.length; i ++) {
    if (cartasrojas[i][0] %2===0 || cartas[i][1] %2===0 ){
        cartasrojasypares[cartasrojasypares.length] = cartasrojas[i];
    } 
}
    console.log(cartasrojasypares[cartasrojasypares.length-1])
VM3280:6 12d
undefined
cartasrojasypares
(12) ["2c", "4c", "6c", "8c", "10c", "12c", "2d", "4d", "6d", "8d", "10d", "12d"]
cartasrojas
(24) ["1c", "2c", "3c", "4c", "5c", "6c", "7c", "8c", "9c", "10c", "11c", "12c", "1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "10d", "11d", "12d"]
cartasnegras
(24) ["1t", "2t", "3t", "4t", "5t", "6t", "7t", "8t", "9t", "10t", "11t", "12t", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p", "12p"]

I'm fine, but I was really asked to use dictionaries or objects, a subject that I have little or nothing clear, now I'm trying to do it that way, so I get to the next code, I have an array of empty letters, an array with the sticks of the card, I create the objects and keep the result in letters and I have the cards left, which is not how to do the filtering I need, 1-on one hand red and black cards, 2-red cards take out the pairs and print the last letter

var cartas = [];

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

for (var i = 0; i < palo.length; i = i + 1) {
      for (var j = 1; j <= 12; j = j + 1) {
        cartas[cartas.length] = { palo: palo[ i ], valor: j };
      }
    }

cartasrojas = []

cartasnegras = []

for (var i = 0; i < cartas.length; i ++) {
    if (cartas[i].palo === "c" || cartas[i].palo === "d") {
        cartasrojas[cartasrojas.length] = cartas[i];
    } else {
       if (cartas[i].palo === "t" || cartas[i].palo === "p") {
        cartasnegras[cartasnegras.length] = cartas[i];
   }
}
}

cartasrojasypares = []

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

console.log(cartasrojasypares[cartasrojasypares.length-1]);
    
asked by Fernando 04.08.2018 в 22:16
source

1 answer

0

You almost have it.

The attributes or functions of an object are accessed by the operator . . In fact, you already use some in your code console.log , cartasrojas.length , etc.

Once you define an object with a cartas[cartas.length] = { p: palo[ i ], v: j }; attribute, you simply do the same console.log(cartas[0].p) 1 .

For example, to get the last card.

    var cartas = [];
    var palo = ["c","d","t","p"];
    for (var i = 0; i < palo.length; i = i + 1) {
      for (var j = 1; j <= 12; j = j + 1) {
        cartas[cartas.length] = { palo: palo[ i ], valor: j };
      }
    }
    var ultimaCartaRoja;
    for (var i = 0; i < cartas.length; i ++) {
       var carta = cartas[i];
       if (carta.palo == "c" || carta.palo == "d"){
           // Es una carta roja
           if (carta.valor % 2 == 0) {
              // Es par
              ultimaCartaRoja = carta;
           }
       }
    } 
    console.log(ultimaCartaRoja);

1 It is always good to use identifiers that give information about what the value means, even if they are a little longer. In the example I have changed p and v for palo and valor , which are read much better.

    
answered by 04.08.2018 / 23:13
source