I've been developing a quiz app using only JavaScript. The app allows users to choose a username, answer questions to increase their score, and saves the data in variables. When the game ends, a message is displayed along with the top 5 scores achieved by users. To store this information, I am utilizing local storage. However, I encountered an issue where the data is being overwritten when the game is restarted or played again. For instance, the first user's name gets replaced by the newer player. Here is the code snippet for saving the data:
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 { "username": user , "score": score};
}