vendredi 30 septembre 2016

Writing random numbers without repeating using localStorage

So I want to write a different random number between 0 and 5 to the of a page everytime I refresh, how can I prevent it for generating a number thats already been generated? I guess I need to use localStorage API for this but i can't wrap my head around it. Here's the code of the script I'm trying:

var random = Math.floor(Math.random() * 6);
var lastRandom = 0;
localStorage.setItem('random', random.toString());
if (localStorage.getItem('random') === lastRandom.toString()) {
    localStorage.setItem('random', lastRandom.toString()) 
}




Aucun commentaire:

Enregistrer un commentaire