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

4
  • 2
    You would be better to ask this on: codereview.stackexchange.com Commented Jun 17, 2014 at 16:44
  • 1
    @MikeCheel Not really, sincethe code is not functional (see meta.stackoverflow.com/questions/253975/… Commented Jun 17, 2014 at 16:46
  • You're missing a closing bracket in that loop btw. Commented Jun 17, 2014 at 16:52
  • Ah you're right... still doesn't work though Commented Jun 17, 2014 at 17:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your demo but nothing returns in the results panel?
@user3743564 check the console instead. It will show the output.
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.

9 Comments

Yeah I realized that, but even with that fix it doesn't seem to work right still.
@user3743564 what do you mean? see this jsfiddle.net/8Ah48/4 just hit run and it seems to be working fine.
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?
@user3743564 yes, one of your url's is expecting a post and the standard http request is a get request.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.