dimanche 2 octobre 2016

local storage Overwriting issue

i have been working on quiz app where i am only allowed to use javaScript. the app works like i ask user to chose username and i save it in variable and he/she starts answering and the score gets higher and higher and being saved in score variable as well. later on once the game finishes i am printing the message and have to present the 5 top score users. for that purpose i am using local storage, the problem is when i save the data it works fine but when i restart or play again the data is being overwritten. for example first user name being replaced with newer player. my code for saving the data is ;

      var obj = convertUserAndScore(user, score);
      var players = new Array;

      players.sort(function(a, b){
        return a.score - b.score;
      });


      players.push(JSON.stringify(obj));

      localStorage.setItem("players", players);


      function convertUserAndScore(user, score) {

         return { "usename": user , "score": score};
      }




Aucun commentaire:

Enregistrer un commentaire