1

I am not understanding how to set and get more the one item per line, like name and score... I only get back the name and last score saved... I need doing it not using JSON :/

var i=0;
function saveData()
{

    var score = (Math.random()*10).toFixed();
    console.log(score);
    var player1 = "player1" + i;
    var score = "score " + i;
    i++;
    player1 = prompt("What is your player1?");
    localStorage.setItem(player1, score);
}

function showData()
{
    var show = '';
    for(var j=0 ; j<=localStorage.length-1; j++){
          key = localStorage.key(j);
          value = localStorage.getItem(key);
          show =  show+  "\n"+key;
    }
    alert(show + "\n"+ value);
}
zero
  • 11
  • 2
  • 3
    Possible duplicate of [how to store an Array in localStorage?](http://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage) – Kenney Nov 29 '15 at 14:44
  • I need doing it not using JSON :/ – zero Nov 29 '15 at 17:07
  • You can already fetch multiple values from the `localStorage`; are you sure you are storing more than 1 value? Have you filled in different values in the `prompt` popup? – Kenney Nov 29 '15 at 17:16
  • What is the reason why you can't or don't want to use JSON? – t.niese Nov 29 '15 at 17:19
  • Ok, I did it working, and now I want it sorting by score, not by name. Is there a way? – zero Nov 29 '15 at 17:28

1 Answers1

0

Ok, I did it working, and now I want it sorting by score, not by name. Is there a way?

    var sacore = (Math.random()*10).toFixed(2);
    console.log(sacore);
    var score = sacore.toString();
    var player1 = ("player1" + score+ "score") + i;
    //var score = "score " + i;
    i++;
    player1 = prompt("What is your player1?");
     player1 = player1 +" "+  (score);
    localStorage.setItem(player1, score);
zero
  • 11
  • 2