0
    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);
    return previous

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

I'm trying to make a code that will be used to lead users to a random site from a set of sites. This will be activated by a button in google sites, I just haven't gotten to the html part. Anyways, when I try to run this code in jsfiddle, the output is just a blank screen. Whats wrong? here's my logic

  1. An array of the set sites.
  2. 'random' picks a number between 0 and 4, which corresponds to the sites in the array
  3. an empty array
  4. This pushes 'random's output to the empty array

  5. This for loop checks to see if there is any data in the empty array

  6. While loop says "ok, if random chooses a number already in the array 'previous', I will run random again.
  7. Once an unchosen number is outputted, a new window opens to the chosen site.

Sadly, it's not performing this way. Any tips?

Edit: Jsfiddle

Lillz
  • 283
  • 2
  • 5
  • 19

2 Answers2

0

I think a slight reworking of the code might do the trick with particular emphasis on removing the loop:

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

function showLink() {
    if (previous.length !== links.length) {
      var random = Math.round(Math.random() * ((links.length - 1) - 0) + 0 );
      if (previous.indexOf(links[random]) > -1) {
        showLink(); 
      } else {
        console.log(links[random], previous)
        previous.push(links[random]);
      }
    } else {
        console.log('No more links');
    }
}

And at this point just keep calling showLink until you run out of links.

Demo

Andy
  • 61,948
  • 13
  • 68
  • 95
0
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];

You were missing a bracket, and you had a return statement before your for loop. So of course it wasn't working since the code under the return statement was unreachable the way you had it written.

Ryan
  • 5,644
  • 3
  • 38
  • 66
  • Yeah I realized that, but even with that fix it doesn't seem to work right still. – Lillz Jun 17 '14 at 18:54
  • @user3743564 what do you mean? see this http://jsfiddle.net/8Ah48/4/ just hit run and it seems to be working fine. – Ryan Jun 17 '14 at 20:08
  • Weird, I guess a restart of chrome fixed it. Anyways, it still doesn't remember what sites a user has been taken to. I think what is happening is this: Each time you click 'run' it resets the 'previous' array, so my loop (whether or not it works) can't check to see what the previous outputs of math.random were. I need to somehow create an array that won't reset each time this code is run (most likely a cookie). Any ideas? edit: do you get the "{"error": "Please use POST request"}" things too? – Lillz Jun 17 '14 at 21:46
  • @user3743564 yes, one of your url's is expecting a `post` and the standard http request is a `get` request. – Ryan Jun 17 '14 at 22:01
  • @user3743564 also, you would have to use iframes or cookies to get persistence since you are doing `window.location` and taking the user away from your page. – Ryan Jun 17 '14 at 22:03
  • Ahhhh it's google. That's why google hasn't been showing up. Any idea how I could set up cookies for this to work? – Lillz Jun 17 '14 at 22:04
  • Didn't see your next comment. Well I am have just started javascript... does window.location open a new window instead of leading away from the page... It would be the best if it could open a new window. That way I wouldn't need cookies, but I'm still not sure how I would get the array to be persistent. – Lillz Jun 17 '14 at 22:06
  • @user3743564 that would be an entirely new question. and not something to go into in the comments. but feel free to ask it after doing some research and ping me (just comment on this answer with the link). Also if any content on the site is helpful make sure to upvote and mark as accepted. – Ryan Jun 17 '14 at 22:07
  • Ok new question is here: http://stackoverflow.com/questions/24274229/persistent-array-in-javascript and thanks, the community here is great for newbies, so I will upvote! – Lillz Jun 17 '14 at 22:18