Skip to main content
fixed formatting
Source Link
hgb123
  • 14.9k
  • 3
  • 24
  • 43

At the moment, I have some code to pick one random object from an array and it works well. However, I don't want any repeats in the output. I want to randomly provide a new site link on the click of a button, and it should be different every time (until of course we run out of objects in the array). What code can I add to this to prevent any repetition?

Here's the sample code:

<script>  var links = ["page-1",[   "page-1",  "page-2",   "page-3",   // ...   "page-108",   ]   function openSite() {   var randIdx = Math.random() * links.length;   randIdx = parseInt(randIdx, 10);   var link = 'https://websitename.com/page/' + links[randIdx];   window.location.assign(link);     }; </script> 

At the moment, I have some code to pick one random object from an array and it works well. However, I don't want any repeats in the output. I want to randomly provide a new site link on the click of a button, and it should be different every time (until of course we run out of objects in the array). What code can I add to this to prevent any repetition?

Here's the sample code:

<script> var links = ["page-1",   "page-2",   "page-3",   ...   "page-108",   ]   function openSite() {   var randIdx = Math.random() * links.length;   randIdx = parseInt(randIdx, 10);   var link = 'https://websitename.com/page/' + links[randIdx];   window.location.assign(link);     }; </script> 

At the moment, I have some code to pick one random object from an array and it works well. However, I don't want any repeats in the output. I want to randomly provide a new site link on the click of a button, and it should be different every time (until of course we run out of objects in the array). What code can I add to this to prevent any repetition?

Here's the sample code:

<script>  var links = [ "page-1",  "page-2", "page-3", // ... "page-108", ] function openSite() { var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); var link = 'https://websitename.com/page/' + links[randIdx]; window.location.assign(link); }; </script> 
added 64 characters in body
Source Link
added 64 characters in body
Source Link
Always Helping
  • 14.6k
  • 4
  • 16
  • 30

At the moment, I have some code to pick one random object from an array and it works well. However, I don't want any repeats in the output. I want to randomly provide a new site link on the click of a button, and it should be different every time (until of course we run out of objects in the array). What code can I add to this to prevent any repetition?

Here's the sample code:

var links = ["page-1", "page-2", "page-3", ... "page-108", ] function openSite() { var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); var link = 'https://websitename.com/page/' + links[randIdx]; window.location.assign(link); };
<script> var links = ["page-1", "page-2", "page-3", ... "page-108", ] function openSite() { var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); var link = 'https://websitename.com/page/' + links[randIdx]; window.location.assign(link); }; </script> 

At the moment, I have some code to pick one random object from an array and it works well. However, I don't want any repeats in the output. I want to randomly provide a new site link on the click of a button, and it should be different every time (until of course we run out of objects in the array). What code can I add to this to prevent any repetition?

Here's the sample code:

var links = ["page-1", "page-2", "page-3", ... "page-108", ] function openSite() { var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); var link = 'https://websitename.com/page/' + links[randIdx]; window.location.assign(link); };

At the moment, I have some code to pick one random object from an array and it works well. However, I don't want any repeats in the output. I want to randomly provide a new site link on the click of a button, and it should be different every time (until of course we run out of objects in the array). What code can I add to this to prevent any repetition?

Here's the sample code:

<script> var links = ["page-1", "page-2", "page-3", ... "page-108", ] function openSite() { var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); var link = 'https://websitename.com/page/' + links[randIdx]; window.location.assign(link); }; </script> 
Source Link
Loading