This is my code so far:
let cities = ["Bhutan", "London", "Paris", "Dubai", "New York City", "Singapore"];
let show = cities[Math.floor(Math.random() * cities.length)];
function myCities (){
if (cities[1]);
location.assign("https://www.google.com/");
}
So far, I do get a random value from my array as intended. However, I want redirect to a site based off of whatever random value I get. How would I go about this?
CodePudding user response:
Append .html
to the random array element and set that as the URL.
location.assign(show '.html');
CodePudding user response:
Observation : No need to put a condition for cities[1]
as it will always work for the element at index 1 in cities array. For random city, you can just normally make the url
by appending it.
You can try this way :
let cities = ["Bhutan", "London", "Paris", "Dubai", "New York City", "Singapore"];
function loadLocationData() {
let show = cities[Math.floor(Math.random() * cities.length)];
location.assign(`https://en.wikipedia.org/wiki/${show}`);
}
<button onclick="loadLocationData()">Load Cities</button>