0

My code below opens a random website from the array but to stop going on the same website is there a way to delete it once it has been visited. Heres my attempt.

<button onclick="randomLink()";>Click here to go somewhere else!</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
var randomLink = function () {
 var links = new Array(); 
 links[1] = "http://google.com";
 links[2]="http://bing.com";
 var max = (links.length)
 var randomNumber = Math.floor(Math.random()*max);
 var link = links[randomNumber];
 links.splice(randomNumber,1);
 $('iframe').attr('src', link);
}
</script>
<iframe src="" name="iframe_a" ></iframe>
asked Feb 11, 2016 at 17:28
3
  • 1
    Define links outside of the function. You're deleting an item using splice but then putting it back when you run the function again. Commented Feb 11, 2016 at 17:30
  • In addition to @MikeC, as a suggestion, don't define arrays with keys. You are missing the links[0] key and you can have problems. It's better if you don't define the key, it will be asigned automatically by javascript Commented Feb 11, 2016 at 17:31
  • Math.floor(Math.random()*max) Can give 2 and will work incorrectly Commented Feb 11, 2016 at 17:31

4 Answers 4

1

Move links outside of the function:

var links = [
 "http://google.com",
 "http://bing.com"
];
var randomLink = function() {
 var max = links.length;
 var randomNumber = Math.floor(Math.random() * max);
 var link = links[randomNumber];
 links.splice(randomNumber, 1);
 $('iframe').attr('src', link);
};
answered Feb 11, 2016 at 17:31
Sign up to request clarification or add additional context in comments.

1 Comment

I think it´s Math.random() * (max-1). You could also just do var link = links.splice(Math.random() * (links.length-1), 1) and remove 4 lines
0

Every time you call the function, all url's are added to the array. Try to define the array outside the function. To delete something call splice!

best, Sebi

answered Feb 11, 2016 at 17:32

Comments

0

This is true code, you have ; after "".

<button onclick="randomLink()">Click here to go somewhere else!</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
var randomLink = function () {
 var links = new Array(); 
 links[1] = "http://google.com";
 links[2]="http://bing.com";
 var max = (links.length)
 var randomNumber = Math.floor(Math.random()*max);
 var link = links[randomNumber];
 links.splice(randomNumber,1);
 $('iframe').attr('src', link);
}
</script>
<iframe src="" name="iframe_a" ></iframe>

answered Feb 11, 2016 at 17:33

1 Comment

That's a minor typo, not the problem.
0

You do it using splice for example we have arr :

arr = [1,2,3,4,5,6]

To remove arr[2] which hold the value of 3 we can do :

arr.splice(2,1);

arr will become [1,2,4,5,6]

3 is the previous index of the deleted value and 1 is how many values to delete after that index .

You can read more about splice in MDN .

answered Feb 11, 2016 at 17:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.