Skip to main content
added 93 characters in body
Source Link
Karan
  • 12.7k
  • 3
  • 31
  • 45

Please consider explanation from comments. Array.prototype.splice()

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // push removed link to linksUsed linksUsed.push(link); // update link link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; //for (let i = 0; i < 5; i++) { // openSite(); //}
<button onclick='openSite()'>Open Site</button>

Please consider explanation from comments. Array.prototype.splice()

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // push removed link to linksUsed linksUsed.push(link); // update link link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; for (let i = 0; i < 5; i++) { openSite(); }

Please consider explanation from comments. Array.prototype.splice()

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // push removed link to linksUsed linksUsed.push(link); // update link link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; //for (let i = 0; i < 5; i++) { // openSite(); //}
<button onclick='openSite()'>Open Site</button>

added 126 characters in body
Source Link
Karan
  • 12.7k
  • 3
  • 31
  • 45

Please consider explanation from comments. Array.prototype.splice()

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // addpush removed link to linksUsed linksUsed.push(link); // update link   var link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; for (let i = 0; i < 5; i++) { openSite(); }

Please consider explanation from comments.

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // add removed link to linksUsed linksUsed.push(link); // update link   var link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; for (let i = 0; i < 5; i++) { openSite(); }

Please consider explanation from comments. Array.prototype.splice()

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // push removed link to linksUsed linksUsed.push(link); // update link link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; for (let i = 0; i < 5; i++) { openSite(); }

Source Link
Karan
  • 12.7k
  • 3
  • 31
  • 45

Please consider explanation from comments.

var links = ["page-1", "page-2", "page-3", "page-108"]; // array to hold used link values which are removed form links. // when links is empty then assign all values back from linksUsed and clear linksUsed. var linksUsed = []; function openSite() { // if links is empty then assign all values back from linksUsed and clear linksUsed. if (links.length == 0) { links = linksUsed; linksUsed = []; } var randIdx = Math.random() * links.length; randIdx = parseInt(randIdx, 10); // splice will remove n items starting from index. // First parameter value is starting index and 2nd parameter is delete count var link = links.splice(randIdx, 1)[0]; // add removed link to linksUsed linksUsed.push(link); // update link var link = 'https://websitename.com/page/' + link; // for testing commented below line and added log // window.location.assign(link); console.log(link); }; for (let i = 0; i < 5; i++) { openSite(); }