Help with exercise in JS creation and use of functions

2

I need help with the following exercise:

Develop a function that punctuates a hand of cards, which has as parameter an array of letters, each one represented by a dictionary with suit and value. When scoring, the cards add their value except if it is an ace that adds 20.

Declare the letters as 1, create the array with all the letters in it and the code, but the code does not give me the desired result and I do not know what I am doing wrong

carta1 = {PALO:"c",VALOR:1}
carta2 = {PALO:"d",VALOR:7}
carta3 = {PALO:"p",VALOR:5}

cartas = [carta1,carta2,carta3];

function puntua(mano){
  var mano = 0
for (var i = 0; i < cartas.length; i ++) {
    if (cartas[i].valor === 1) {
        mano = mano + cartas[i].valor + 20;
    } else {
    if (cartas[i].valor != 1) {
        mano = mano + cartas[i].valor;
}
   console.log(puntua(mano))
}
}
}    
    
asked by Fernando 07.08.2018 в 01:05
source

2 answers

0

You were missing two things:

  • Javascript is case sensitive. It means that it is not the same case as lower case.

  • You only need one condition. If it is one, you add 20, all the other cases add up their own value.

  • It would be something like this:

    carta1 = {PALO:"c",VALOR:1};
    carta2 = {PALO:"d",VALOR:7};
    carta3 = {PALO:"p",VALOR:5};
    
    cartas = [carta1,carta2,carta3];
    
    function puntua(){
      var mano = 0;
      for (var i = 0; i < cartas.length; i ++) {
        if (cartas[i].VALOR == 1) {
            mano = mano + 20;
        } else {    
            mano = mano + cartas[i].VALOR;
        }
       
      }
      console.log(mano);
    }
    
    puntua();
        
    answered by 07.08.2018 / 01:15
    source
    0

    Try this code

    // first.

    puntuar=function(mano){
        var total=0;
         for (i=0; i< mano.length; i++){
         if(i.valor ===1){
                total += 20;
    } else{
                total += i.valor;
    }} 
    return total;
    };
    

    // second.

    puntuar_doble_rojo=function(mano){
        var total=0;
         for (i=0; i< mano.length; i++){
          var mulitplic=1;
         if(i.palo==='c' || i.palo==='d'){
            mulitplic=2;
    }
         if(i.valor ===1){
                total += 20*multiplic;
    } else{
                total += i.valor;
    }} 
    return total;
    };
    

    // third.

    puntuar_doble_rojo=function(mano1,mano2){
    var ganador= 'Ganó el jugador 1';
    if (puntuar_doble_rojo(mano2) > puntuar_doble_rojo(mano1)){
    ganador= 'Ganó el jugador 2';
    }else if (puntuar_doble_rojo(mano2) === puntuar_doble_rojo(mano1)) {
    ganador="Fue un empate";
    }
    console.log(ganador);
    }
    
        
    answered by 07.08.2018 в 01:14