I need to write a suit functionDeruco, that given a suit, returns a list of strings, one for each card of this suit following the cards of the truco

Asked

Viewed 4,717 times

3

TIP: The cards include all numbers except cards 8 and 9.

The reasoning I had at first was to create several arrays each with the suit name (spades, clubs, diamonds and hearts) and then use the FOR repeat command to return the string list. Until the end of this racicinio I found it quiet, but I (literally) enclosed in the FOR part and the exclusion of cards 8 and 9. Can anyone help me in this code using the simplest way to initiate? (I am learning alone and through the internet, patience rs).

In the FOR part I did this for each suit, however appear errors like: (naipeDeruco makes comparisons against strings) and naipeDeruco("swords") SHOULD NOT contain the "8 of spades" - naipeDeruco(...). indexof is not a Function //

I don’t really know how to solve this exercise even more than the error of comparisons against strings is already recurring in other exercises of conditionals that I did.

    function naipeDeTruco(naipe){
  let espadas = [];
  let copas =   [];
  let ouros =  [];
  let paus  =  [];

for(var i=0; i <13; i++)
  if(naipe === "espadas"){
   naipe =[ i +" "+"de" + naipe] - [ 8 +" "+"de" + naipe] - [ 9 +" "+"de" + naipe];
    return naipe;
  } 
}
  • 1

    You can give an example of what it is naipe and what you expect this function to return to?

  • Suit is the name given to the "families" or types of playing cards (spades, clubs, diamonds and hearts). An example that is given is: naipeDeruco("swords") will return ["1 of swords", "2 of swords","3 of swords" ...,"12 of swords"]

  • Okay, and no return King, Ace, Queen or Jack?

  • return a list of strings, one for each card of that suit following the cards of the truco. From what I understand in the question he asks all the cards of each suit but excluding the cards 8 and 9, so should enter yes King, Ace, Queen and Jack.

  • What are their letters and orders? They must be returned in order of strength?

  • all cards contained in a suit except cards 8 and 9. They must not be returned in order of strength, the only restriction given is on cards 8 and 9. And by the given example goes up to 12 cards. (I’ve never played cards so I’m more confused)

Show 1 more comment

3 answers

3

The most logical way, since the type of cards is pre-played is to have an array "prepared" and return only with suit indication... ie:

function naipeDeTruco(naipe) {
  return ["1", "2", "3", "4", "5", "6", "7", "10", "11", "12", "Valete", "Rainha", "Rei", "Às"].map(function(tipo) {
    return [tipo, naipe].join(' de ');
  });
}

console.log(naipeDeTruco('espadas'));
console.log(naipeDeTruco('copas'));

  • I ran a test on your code and there were some mistakes I didn’t understand. naipeDeruco("swords") should be ["1 of spades", "2 of spades"... [ '1 of spades', '2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades', deepEqual [ '1 of spades', '2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades',

  • 1

    @Stéphanieverissimo, I think it is worth editing your question showing how you are doing these tests. ;)

  • 1

    I discovered the mistake! I delete "Jack", "Queen", "King", "Ace" to see if it changed something and it really worked! I’ll search on . John and . map to better understand your code!

3


My code was a little different from yours, instead of asking only to write, I put to add to the Array, I did not need to create an array for each suit. Is working properly!

My thanks to the code of the colleague who asked if the course was from Digital House, which gave me a light to resolve!

copas = '♥';
ouros = '♦';
paus = '♣';
espadas = '♠';
function naipeDeTruco(naipe) {
  var resultado = [];
  for (var i = 1; i <= 12; i++) {
    if (i == 8 || i == 9) {
      i = i + 1;
    } else {
      resultado.push(i + " de " + naipe);
    }
  }
  return resultado;
}
console.log(naipeDeTruco(copas));
console.log(naipeDeTruco(ouros));
console.log(naipeDeTruco(paus));
console.log(naipeDeTruco(espadas));

2

Excluding "Jack", "Queen", "King", "Ace" the code worked!

function naipeDeTruco(naipe) {
  return ["1", "2", "3", "4", "5", "6", "7", "10", "11", "12"].map(function(tipo) {
    return [tipo, naipe].join(' de ');
  });
}

console.log(naipeDeTruco('espadas'));
console.log(naipeDeTruco('copas'));

Browser other questions tagged

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