Lillz
Lillz

Reputation: 283

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.push(random);

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

So I've got this here code. It's purpose is, once launched with a button on a google site, to lead the user to one of the set sites, randomly. What I need it to is to remember which site it takes them to (by remembering the Math.random output). Right now, each time the code is run (simulating a user clicking the button many times), it erases my memory array, 'previous'. I want this code to open a separate window for the site it outputs to. Whether cookies, iframes or some other method is used, I'd be very thankful if someone could help me out.

I'm currently going through the Codecademy course on Javascript, so please understand if I am missing something simple :)

Upvotes: 1

Views: 2587

Answers (2)

Ryan Erdmann
Ryan Erdmann

Reputation: 1826

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:

function contains(array, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == value) return true;
    }
    return false;
}

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 again
    if (visited.length == links.length) {
        alert('You visited all the links');
        return;
    }

    // Variables to hold our random number and link
    var random, url;

    // Keep getting a new random url while it's one we've already visited
    do {
        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 window
    var win = window.open(url, '_blank');
    win.focus();
});

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

Upvotes: 0

Kamrul
Kamrul

Reputation: 7301

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];

Upvotes: 2

Related Questions