Write a Naipedetruco(suit) function that returns an array of strings with the numbers + suits of the cards

Asked

Viewed 290 times

1

I’m solving the exercise below, but I’m having some problems:

Write a suit functionDeruco, which given a suit, returns a list of strings, one for each card of this suit following the cards of the card. Ex:

naipeDeTruco("espadas")

["1 de espadas", "2 de espadas", "3 de espadas" ..., "12 de espadas"]

Remember that truco cards include all cards numbered 1 to 12, with the exception of cards 8 and 9.

Obs:

  1. Remember that we will see as a result a set of strings, in this case, greatly respect the concatenation of spaces, and the uppercase and lowercase letters.

  2. As a counter in this case you will have to create an array within the function. Similarly, you have to put each of the matching strings and at the end of the function you have to return the counter.

  3. Remember not to put a specific data as a parameter, so our function has to serve all suits of the deck.

Here is my code:

function naipeDeTruco(naipe){

    var arrayDeNaipes= [];

    for (var i=1; i <=12; i++){

      if((naipe == "espadas") && ([i]!=8) && ([i]!=9) && ([i]!=10)){
        arrayDeNaipes.push([i]+ " de espadas");
        }   
            else if((naipe == "paus") && ([i]!=8) && ([i]!=9) && ([i]!=10)){
            arrayDeNaipes.push([i]+ " de paus");
            } 
                else if((naipe == "copas") && ([i]!=8) && ([i]!=9) && ([i]!=10)){
                arrayDeNaipes.push([i]+ " de copas");
                } 
                    else if((naipe == "ouro") && ([i]!=8) && ([i]!=9) && ([i]!=10)){
                    arrayDeNaipes.push([i]+ " de ouro");
                    } 
         }
    return arrayDeNaipes;
}

Test my code on this site (https://jsfiddle.net/) and it’s working, it’s returning what’s expected:

inserir a descrição da imagem aqui

However, when I will validate it on the digital house platform, the following error appears:

inserir a descrição da imagem aqui

NOTE: In the statement it is requested that the numbers 8 and 9 are not returned. In my code, I put not to return the 8 and 9 and gave the error above. Then I modified it to not return 8, 9 and 10, but it gives the same error.

Does anyone know what might be going on?

  • your code doesn’t really return, you should talk to your course support to check the code on their side

  • You can simplify that lot of if: https://jsfiddle.net/x54Lozfh/

  • Hi @hkotsubo, thanks for the help. I did so below and passed the test. function naipeDeTruco(naipe){&#xA; var naipeEscolhido = naipe;&#xA;&#xA; var cartas = [&#xA; "1 de "+naipeEscolhido, "2 de "+naipeEscolhido, "3 de "+naipeEscolhido, &#xA; "4 de "+naipeEscolhido, "5 de "+naipeEscolhido, "6 de "+naipeEscolhido, &#xA; "7 de "+naipeEscolhido, "10 de "+naipeEscolhido, "11 de "+naipeEscolhido, "12 de "+naipeEscolhido];&#xA;&#xA; return cartas;&#xA; }&#xA;&#xA;console.log(naipeDeTruco("espadas"));

2 answers

1

I appreciate your time in helping me. I found this solution here. I removed the Ifs, passed the parameters and concatenated the strings together.

    function naipeDeTruco(naipe){
     var naipeEscolhido = naipe;

       var cartas = [
       "1 de "+naipeEscolhido, "2 de "+naipeEscolhido, "3 de "+naipeEscolhido, 
       "4 de "+naipeEscolhido, "5 de "+naipeEscolhido, "6 de "+naipeEscolhido, 
       "7 de "+naipeEscolhido, "10 de "+naipeEscolhido, "11 de "+naipeEscolhido, "12 de "+naipeEscolhido];

        return cartas;
    }

console.log(naipeDeTruco("espadas"));

-1

function naipeDeTruco(naipe){

var arrayDeNaipes= [];

for (var i=1; i <=12; i++){

  **if((naipe == "espadas") && ([i]!=8) && ([i]!=9))**{//Retira o !=10 dessa parte do código
    arrayDeNaipes.push([i]+ " de espadas");
    }   
        else if((naipe == "paus") && ([i]!=8) && ([i]!=9 && [i]!=10)){
        arrayDeNaipes.push([i]+ " de paus");
        } 
            else if((naipe == "copas") && ([i]!=8) && ([i]!=9 && [i]!=10)){
            arrayDeNaipes.push([i]+ " de copas");
            } 
                else if((naipe == "ouro") && ([i]!=8) && ([i]!=9 && [i]!=10)){
                arrayDeNaipes.push([i]+ " de ouro");
                } 
     }
return arrayDeNaipes;
}
console.log(naipeDeTruco("espadas"))

Browser other questions tagged

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