Calculate total points

0

Hello I would need help or some idea for how to calculate the total score of this card game in JavaScript, the problem is that every time I call the score function I reset the internal variables and I can not create the total score

// Funcion puntuacion y calculos
function puntuacion(array) {


    var ultimaTirada = 0;
    var puntuacionTotal = 0;
    var valColor1 = 0;
    var valColor2 = 0;
    var valColor3 = 0;
    var valColor4 = 0;

    // Hacemos el calculo de que si el color que esta en esa posicion de la array es ese, que haga el caluclo correspondiente

    for ( var i = 0; i < array.length; i++){


                if (array[i] == 'red'){                    
                    valColor1 += -2 ;

                } else if(array[i] == 'green'){

                    valColor2 += 3;

                } else if(array[i] == 'blue'){

                    valColor3 += 2;

                } else if(array[i] == 'yellow'){

                    valColor4 += 1;
                }      

                 ultimaTirada = valColor1 + valColor2 + valColor3 + valColor4;


         }
         // Guardo la ultima tirada y voy sumando

        totalScore += ultimaTirada; // Aquí tengo el error // 

    // Pinto la ultima tirada y el total del score
    document.getElementById("score").innerHTML = "Valor de la ultima Tirada: <b>" + ultimaTirada + " </b><br>Puntuación Final: " + puntuacionTotal;

}

// Colores random 
/* Math.random() entre 5 color azul,rojo,verde,amarillo, 
El color rojo resta puntos*/
function coloresRandom() {

    // Ocultamos los bototnes de modos de juego
    if (document.getElementById("C2").style.visibility == 'visible'){
        document.getElementById("jugar3").style.visibility = 'hidden';
        document.getElementById("jugar4").style.visibility = 'hidden';
        document.getElementById("jugar5").style.visibility = 'hidden';
    }

    // Variable para random
    var numRandom;

    // Blue
    // Red
    // Green
    // Yellow
    var arrayColores = ["url('http://3.bp.blogspot.com/-jUiUFYNSktQ/Vi-KImE5GnI/AAAAAAAErfM/8n2P2wH_sug/s1600/FONDOS%2BPARA%2BTEL%25C3%2589FONOS%2B%252843%2529.gif')",
                        "url('http://2.bp.blogspot.com/-QUpi_xZM79o/Vi-KI9pd8ZI/AAAAAAAErfQ/eS4pxUlJu4Q/s1600/FONDOS%2BPARA%2BTEL%25C3%2589FONOS%2B%252844%2529.gif')", 
                        "url('https://k36.kn3.net/3/1/4/4/7/8/23F.gif')",
                        "url('https://k36.kn3.net/9/F/8/9/6/2/B26.gif')"];

    var colores = ['','','',''];
    // id name
    var carta1 = document.getElementById("C1");
    var carta2 = document.getElementById("C2");
    var carta3 = document.getElementById("C3");
    var carta4 = document.getElementById("C4");
    var carta5 = document.getElementById("C5");

    // Contador
    var contador = 0;
    // Miramos si es visible , si lo es la contamos 

    if (carta1.style.visibility == 'visible' && carta2.style.visibility == 'visible' && carta3.style.visibility == 'visible' && carta4.style.visibility == 'visible' && carta5.style.visibility == 'visible') {
        contador = 5;
    }else if (carta1.style.visibility == 'visible' && carta2.style.visibility == 'visible' && carta3.style.visibility == 'visible' && carta4.style.visibility == 'visible') {
        contador = 4;

    } else if (carta2.style.visibility == 'visible' && carta3.style.visibility == 'visible' && carta4.style.visibility == 'visible') {
        contador = 3;

    }

    for (var i = 0; i < contador; i++) {        
        numRandom = Math.floor((Math.random() *4));
        // Guardar en cada numero un color
        if(numRandom == 0){
            colores[i] = 'red';
        } else if(numRandom == 1){
            colores[i] = 'green';
        } else if(numRandom == 2){
            colores[i] = 'blue';
        } else if(numRandom == 3){
            colores[i] = 'yellow';
        }

        if (i == 3) {       
            carta1.style.backgroundImage = arrayColores[numRandom];

        } else if (i == 0) {
            carta2.style.backgroundImage = arrayColores[numRandom];

        } else if (i == 1) {
            carta3.style.backgroundImage = arrayColores[numRandom];

        } else if (i == 2) {
            carta4.style.backgroundImage = arrayColores[numRandom];

        } else if (i == 4) {
            carta5.style.backgroundImage = arrayColores[numRandom];
        }


    }

    puntuacion(colores);    

}

    
asked by Eduard Nastrut 15.03.2018 в 16:19
source

2 answers

0
var total = 0;
// Función para mostrar por pantalla el resultado tanto de la ultima tirada como el total
function puntuacionTotal(){
    // Invocamos la puntuacion de la ultima tirada y la guardamos en una variable
    var ultima = puntuacion(coloresRandom());
    // hacemos la suma
    total = ultima + total;
    // Lo mostranmos
    document.getElementById("puntos").innerHTML = "Ultima Tirada: " + ultima + 
        "
Total: " + total; }
    
answered by 19.03.2018 / 10:07
source
0

The problem is in what is known about the scope of the function, more specifically you are initializing the variables in the scoring function so that each time the flame is initialized again. One way to solve this is to use global variables.

Example:

// Inicializar

var puntuacionTotal = 0;
document.querySelector('input').addEventListener('click',puntuacion);

// Funciones
function puntuacion(){
  puntuacionTotal += 1; 
  console.info(puntuacionTotal);
};
<input type="button" value="Suma 1">
    
answered by 15.03.2018 в 21:21