Help with JavaScript exercise

1

I need help with the following exercise: It is an exercise using JS in the browser console, I have 3 cards, I have to create a function and add them with the following condition, if the card is a 1 value of 20 and if it is a red card it is worth twice its value.

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) {
        cartas[i].VALOR = 20;
    }
    if (cartas[i].PALO === "d" || cartas[i].PALO === "c") {
        cartas[i].VALOR = cartas[i].VALOR*2;
    }
    for (var i = 0; i < cartas.length; i ++) {   
        mano = mano + cartas[i].VALOR;
    }
   
  }
  console.log(mano);
}

puntua();

As a result I get 52, I suppose that 1 of hearts is giving it to me as 40 because 1 is worth 20 and being red is worth double, but I do not know why the other 2 cards are only 5 and 7 when the 7 that is a "d" I would have to add it to 14, someone can tell me what is wrong in my code

    
asked by Fernando 07.08.2018 в 15:10
source

2 answers

2

Apart from not working well, you have another problem: you are modifying the value of the cards, what you should avoid: If you want to check the value twice, the second time will multiply the value of the red cards by two .

Using the divide and conquer strategy, you can first create a function that calculates the value of a card without modifying it and then use it to add a set of cards.

In my solution, I'm using the Array.reduce() method because it's so simple, saving you until the loops:

let carta1 = {PALO:"c",VALOR:1}
let carta2 = {PALO:"d",VALOR:7}
let carta3 = {PALO:"p",VALOR:5}
let cartas = [carta1,carta2,carta3];

function puntuaCarta(c) {
  let v = c.VALOR === 1 ? 20: c.VALOR;
  if (c.PALO === 'c' || c.PALO === 'd') {
    v = v * 2; //duplicamos
  }
  console.log('Vale',v);
  return v;
}

function puntua(cartas){
  return cartas.reduce(
    (acumulador,c ) => acumulador + puntuaCarta(c), //función que acumula
    0 //valor inicial del acumulador
  );
}

console.log('Puntuación',puntua(cartas));

But if you want a classic loop (totally valid, it's a matter of taste), you can do it like this:

let carta1 = {PALO:"c",VALOR:1}
let carta2 = {PALO:"d",VALOR:7}
let carta3 = {PALO:"p",VALOR:5}
let cartas = [carta1,carta2,carta3];

function puntuaCarta(c) {
  let v = c.VALOR === 1 ? 20: c.VALOR;
  if (c.PALO === 'c' || c.PALO === 'd') {
    v = v * 2; //duplicamos
  }
  console.log('Vale',v);
  return v;
}

function puntua(cartas){
  var mano = 0;
  for (let i=0;i<cartas.length;i++) {
    mano+=puntuaCarta(cartas[i]);
  }
  return mano;
}

console.log('Puntuación',puntua(cartas));
    
answered by 07.08.2018 / 15:42
source
2

I already fixed it, I hope it works for you, you had a for cycle that should not be.

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

function puntua(){
  var mano = 0;
  cartas.forEach(function(elemento) {
    var carta = elemento.VALOR == 1 ? 20 : elemento.VALOR;
    if (elemento.PALO == "d" || elemento.PALO == "c")
      carta = carta * 2;
    console.log(carta);
    mano += carta;
  });
  console.log(mano);
}
puntua();
    
answered by 07.08.2018 в 15:24