Skip to content Skip to sidebar Skip to footer

Persistent Array In Javascript

var links = ['http://www.google.com/', 'http://www.cnn.com/', 'http://www.bbc.com/', 'http://www.nbc.com/']; var random = Math.round(Math.random() * 4); var previous = []; previous

Solution 1:

you need a global previous.

use

var links = ["http://www.google.com/", "http://www.cnn.com/", "http://www.bbc.com/", "http://www.nbc.com/"];
var random = Math.round(Math.random() * 4);
window.previous = window.previous || [];
window.previous.push(random);

for (var i = 0; i < window.previous.length; i++) {
    while (window.previous[i] == random) {
        random = Math.round(Math.random() * 4);
    }
}
window.location = links[random];

Solution 2:

You have a couple of problems with your solution:

  1. When you navigate to a different page (with window.location = ...), you lose any variables you have. It's impossible to keep your previous array if you navigate to a new page, even if you attach it to the window.
  2. Your for loop with a while isn't doing what you think. It should make sure you get a number you haven't used yet, but there's a bug: the inner while can choose a random number that is in the previous array after you've gone past in in the for. i.e. if previous = [0,2], when the for loop is checking that previous[1] == random, it could choose 0 as the new random number, even though it's the value of previous[0].
  3. You'll infy-loopy if you've visited all the links.

To fix this, first you have to start opening the pages in a new window. Refer to this SO answer for more information on how to do this.

Second, you need to do a better job of making sure that your previous array doesn't contain the value. A simple implementation of a contains function is:

functioncontains(array, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == value) returntrue;
    }
    returnfalse;
}

Here's a working JS Fiddle of what you're looking for: http://jsfiddle.net/pnP4D/7/

I keep a visited array to store the links you've visited; you could just as easily keep this as your previous array and store random numbers.

var links = ["http://www.google.com/", "http://www.cnn.com/", "http://www.bbc.com/", "http://www.nbc.com/"];
var visited = [];

// Assumes you have an element like <button id='btn'>Click me</button>var button = document.getElementById('btn');
button.addEventListener('click', function() {

    // If we've visited all the links, don't try redirecting againif (visited.length == links.length) {
        alert('You visited all the links');
        return;
    }

    // Variables to hold our random number and linkvar random, url;

    // Keep getting a new random url while it's one we've already visiteddo {
        random  = Math.round(Math.random() * 3);
        url = links[random];        
    } while (contains(visited, url));

    // We have a url we haven't visited yet; add it to the visited array
    visited.push(url);   

    // Open the link in a new window so we can hold on to the visited array in this windowvar win = window.open(url, '_blank');
    win.focus();
});

// A simple function to check if an array contains a valuefunctioncontains(array, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == value) returntrue;
    }
    returnfalse;
}

Post a Comment for "Persistent Array In Javascript"