Save two values and two scores in localstorage

2

I would like to keep two names entered and their two scores in localstorage and I can only do it in one. I do not know if it's done with JSON because I'm very beginner, so excuse my inexperience. The idea is to save name1 with your score sum1 and name2 with your score sum2. The score I have not managed to do so I do not put that code. I leave the little that I have done (I call in that place the functions to be tested):

function saveNames() {
    localStorage.setItem('nomUsuari', name1);
    localStorage.setItem('nomUsuari2', name2);
}   

function obtener() {
    var nombreGuardado = localStorage.getItem('nomUsuari');
    var nombreGuardado2 = localStorage.getItem('nomUsuari2');
}

var name1 = prompt("Nombre jugador uno");


var name2 = prompt("Nombre jugador dos");
saveNames();
obtener();

var cards = new Array(
    new Array("manzana", 1),
    new Array("manzana", 2),
    new Array("manzana", 3), 
    new Array("pera", 1),
    new Array("pera", 2),
    new Array("pera", 3),
    new Array("melon", 1),
    new Array("melon", 2),
    new Array("melon", 3)
);

var cards_views_ids1 = Array();
var cards_views_ids2 = Array();
var suma = 0;
var suma1 = 0;
var suma2 = 0;

function random_card(num){
    if(cards.length >0 ){
        var rand = Math.floor(Math.random()*cards.length);
        var palos = "<br>Palo: "+cards[rand][0]+"<br><br> Valor:";
        var numero = cards[rand][1];
        document.getElementById("div"+num).innerHTML += palos;
        document.getElementById("div"+num).innerHTML += numero;

        var repetido = 0;

        switch(num) {
            case 1:
            suma1++;
            suma = suma1;
            cards_views_ids1.push(numero);     

            cards_views_ids1.forEach(function(dato){
                if (dato == numero){
                    repetido++;
                  }
            });
            break;
            case 2:
            suma2++;
            suma = suma2;
            cards_views_ids2.push(numero);

            cards_views_ids2.forEach(function(dato){
                if (dato == numero){
                  repetido++;
                 }
           });
           break;
    }  

 if(repetido == 2){
    const buttons=document.getElementsByTagName('button');
    buttons[num-1].setAttribute('disabled','disabled')

    setTimeout(function(){
        var jugador = document.getElementById("player"+num+"_result");
        if (jugador.innerHTML == "0"){
              var puntua ='Se repitió el valor <br> fruta: ' + cards[rand][0] +
              ' - Valor: ' +    cards[rand][1] +'. Se extrayeron ' + suma +
              ' valores hasta encontrar el valor repetido.';
              jugador.innerHTML=puntua;

        }

      }, 100);  

  }

}

}


 <div id="start_form"></div>
 <div id="div1"></div>
 <div id="div2"></div>
 <div id="game_main">

 <div id="boton"><button onclick="random_card(1);">Extraer</button>
 <span id="player1"></span></div><span id="player1_result">0</span>
 <div id="boton2"><button onclick="random_card(2);">Extraer</button>
 <span id="player2"></span></div><span id="player2_result">0</span>
 </div>
    
asked by Vero Soler 31.05.2018 в 00:52
source

1 answer

0

First, you say that only a name is saved but it is not, with your code they are already saving both:)

Regarding saving the sum, a simple option is to do the same as with the names just when you assign the value to suma :

 suma = suma2;
 localStorage.setItem('suma2', suma);

The value will be updated with each call to the function random_card()

Regarding saving the information in a JSON, I do not know to what extent you need to save data. If it's just those fields, I think it's not worth your trouble.

Here is a example

If you have any questions, tell me and update the answer;)

I publish with your comment:

I think the problem is that by default Chrome does not have the entire grid with the values of localstorage , initially it looks like this:

But if you deploy you can see all:

    
answered by 31.05.2018 / 10:14
source